views:

27

answers:

0

Hi all,

I'm using a DetailsView Control to perform an insert into my database. I've no problems doing this when I set the fields for the DetailsView control on the aspx page....

<asp:TextBox ID="InsertFirstName" runat="server" Text='<%# Bind("First_Name") %>'></asp:TextBox>

...but I need to be able to do this dynamically in the code behind. I'm using a class which implements ITemplate but I can't seem to be able to link the textbox I add dynamically to its related column in the database. Here is my class, which implements ITemplate...

public class InsertTemplate : ITemplate
{
    private string _DataField = "";
    public string DataField
    {
        get { return _DataField; }
        set { _DataField = value; }
    }

    private TextBox _TemplateTextBox = new TextBox();
    public TextBox TemplateTextBox
    {
        get { return _TemplateTextBox; }
        set { _TemplateTextBox = value; }
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        TextBox field_txtbox = new TextBox();
        field_txtbox.ID = DataField;
        field_txtbox.Text = String.Empty;
        container.Controls.Add(field_txtbox);
    }

    private void OnDataBinding(object sender, EventArgs e)
    {
        object boundValueObj = null;
        Control ctrl = (Control)sender;
        GridViewRow dataItemContainer = (GridViewRow)ctrl.NamingContainer;
        boundValueObj = DataBinder.Eval(dataItemContainer.DataItem, DataField);

        TextBox fieldTxtbox = (TextBox)sender;
        if (boundValueObj != null)
        {
            fieldTxtbox.Text = boundValueObj.ToString();
        }
    }
}

So basically I want to be able to perform the same operation that <%# Bind("First_Name") %> performs but I want to be able to do it dynamically in the code behind.