Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# + Linq = VFP 10
Message
From
03/04/2006 19:55:18
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01109804
Message ID:
01110131
Views:
45
It's best not to look back too often but that is so easy in VFP.

Yes, but you have to remember that you're comparing apples and oranges. With VFP, you're talking about Windows Forms. There is no databinding to an ASP page in VFP. So, if you want a fair comparison, you'd have to be talking about .NET WinForms and not WebForms.

They say that databinding in ASP.NET is supposed to be better in 2.0 than it was in 1.1, but I don't have any experience at all with 2.0 WebForms. I *do* remember getting it to work ok though in 1.1.

In fact, I just dug up some stuff I wrote about 2 years ago about databinding on WebForms. I'm just going to copy/paste it here in it's entirety ... maybe it give you some ideas (I'm not even going to re-read before I post it, so I hope it'll help).


Your Page or UserControl Init handles the databinding from data source to display and the Page/UserControl Load would handle it in the other direction, posting back to the data source. The trick is to have stuff in all your "base" classes that handle databinding. I would also define an IBindable interface and have everything that can be databound implement that interface. Keep in mind that the main reason I'm doing it this way is to keep the same paradigm that we have with our WinForm portion of the app. Also, we haven't actually gotten around to implementing any of the web side of things because we've been focusing on WinForms initially, but this methodology *does* work ... there just may be other ways of doing it. Personnally though, I think this is a rather elegant solution. =)

Minimally, I'd recommend something like this for the Interface:
	public interface IBindable
	{
		void DataBind(DataTable dt, string column);
		void DataBind(DataTable dt, string column, int row);
		void DataPostBack();

		DataTable BoundTable
		{
			get;
			set;
		}
		string BoundColumn
		{
			get;
			set;
		}
		int BoundRow
		{
			get;
			set;
		}
	}
Here's an example of a textbox:
public class MyTextBox : System.Web.UI.WebControls.TextBox, IBindable
{
	#region Declarations

	private DataTable m_BoundTable  = null;
	private string    m_BoundColumn = "";
	private int       m_BoundRow    = 0;

	#endregion

	#region Properties

	public DataTable BoundTable
	{
		get {return this.m_BoundTable;}
		set {this.m_BoundTable = value;}
	}
	public string BoundColumn
	{
		get {return this.m_BoundColumn;}
		set {this.m_BoundColumn = value;}
	}
	public int BoundRow
	{
		get {return this.m_BoundRow;}
		set {this.m_BoundRow = value;}
	}

	#endregion

	#region Contructor

	public MyTextBox()	
	{
		this.DataBinding += new System.EventHandler(this.DataBindingHandler);
		this.EnableViewState = false;
	}

	#endregion

	#region Methods

	public void DataBind(DataTable dt, string column)
	{
		this.DataBind(dt, column, 0);
	}

	public void DataBind(DataTable dt, string column, int row)
	{
		this.m_BoundTable = dt;
		this.m_BoundColumn = column;
		this.m_BoundRow = row;
	}

	public void DataPostBack()
	{
		if (this.m_BoundTable != null && this.m_BoundTable.Rows.Count > this.m_BoundRow)
			if (this.m_BoundTable.Rows[this.m_BoundRow][this.m_BoundColumn].ToString() != this.Text)
				this.m_BoundTable.Rows[this.m_BoundRow][this.m_BoundColumn] = this.Text;
	}

	#endregion

	#region Events

	protected virtual void DataBindingHandler(object sender, System.EventArgs e)
	{
		if (this.m_BoundTable != null && this.m_BoundTable.Rows.Count > this.m_BoundRow)
			this.Text = this.m_BoundTable.Rows[this.m_BoundRow][this.m_BoundColumn].ToString();
	}

	#endregion
		
}
For container controls, the DataPostBack() method would be a little different. Something like this:
	public virtual void DataPostBack()
	{
		for (int i=0; i < this.Controls.Count; i++)
		{
			if (this.Controls[i] is IBindable)
				((IBindable)this.Controls[i]).DataPostBack();
		}
	}
Then, in your Page/UserControl Init:
	// get your data
	this.oData = Page.Session["oData"];

	// Databind your controls
	this.MyText1.DataBind(this.oData.Tables["MyTable"], "MyColumn1");
	this.MyText2.DataBind(this.oData.Tables["MyTable"], "MyColumn2");
	
	Page.DataBind();
And in the Page/UserControl Load:
	this.DataPostBack();

	Page.Session["oData"] = this.oData;
I hope this gives you a few ideas.

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform