0

Hi, I have a gridview to which I'm adding template fields programmatically. Each of the template fields have a textbox. I would like to make this text box have 2-way binding to a database column. Please see below code.

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();        
    }
}

I'm calling the above class as follows

tf = new TemplateField();
    tf.HeaderText = "My First Names";
    tf.EditItemTemplate = new CustomEditItemTemplate(DataControlRowType.DataRow, "firstName");
    dvModify.Fields.Add(tf);

How can I make the text box such that when I edit the text, this change is reflected in the database as well?

Thanks for your time everyone

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.