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.