1 Replies - 2453 Views - Last Post: 08 February 2010 - 03:04 PM Rate Topic: -----

#1 dumbncool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-August 09

How to 2-way bind a textbox in a template fields programmatically.

Posted 06 February 2010 - 09:38 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: How to 2-way bind a textbox in a template fields programmatically.

#2 gildebrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-July 09

Re: How to 2-way bind a textbox in a template fields programmatically.

Posted 08 February 2010 - 03:04 PM

Oh my, there is another person that want to do exactly what i want to be able to do. I push this thread up a bit. Please, someone, HELP!
If you solve this, could you please let me know?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1