我有一个gridview,我在其中以编程方式添加了模板字段。每个模板字段都有一个文本框。我想让这个文本框具有双向绑定到数据库列的功能。请参阅下面的代码。如何以编程方式在模板字段中双向绑定文本框
public class CustomEditItemTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public CustomEditItemTemplate(DataControlRowType type, string colname)
{
this.templateType = type;
this.columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
TextBox tb = new TextBox();
tb.ID = columnName;
tb.DataBinding += new EventHandler(this.tb_DataBinding);
container.Controls.Add(tb);
}
private void tb_DataBinding(Object sender, EventArgs e)
{
TextBox t = (TextBox)sender;
DetailsView dv = (DetailsView)t.NamingContainer;
//This line does only one way binding. It takes the rows from the database and displays
//them in the textboxes. The other way binding is not done. This is why my code fails
t.Text = DataBinder.Eval(dv.DataItem, columnName).ToString();
}
}
我打电话上面的类如下
tf = new TemplateField();
tf.HeaderText = "My First Names";
tf.EditItemTemplate = new CustomEditItemTemplate(DataControlRowType.DataRow, "firstName");
dvModify.Fields.Add(tf);
如何使文本框中,这样当我编辑文本,这种变化反映在数据库以及?
感谢您的时间大家