How to get two-way databinding work in codebehind (2.0)?

Discussion in 'ASP .Net' started by dieter@schuele.name, Nov 16, 2005.

  1. Guest

    Two-way databinding (as described in
    http://dotnetjunkies.com/QuickStartv20/aspnet/doc/data/templates.aspx)
    works fine for me if I use it within aspx-files.
    However, I would like to use it my codehind file and I don't know how
    to get it work since I did not find any equivalent for "Bind" to use in
    my ITemplate derived class.

    For my ItemTemplate, I use
    (int)((DataRowView)container.DataItem)[column] as equivalent to <%#
    Eval ...%>

    But was is the equivalent for <%# Bind ...%> to use in the
    EditItemTemplate?


    Any help/suggestions would be greatly appreciated.

    Dieter




    Working code in aspx:

    <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="UserID"
    DataSourceID="DBUserFunctionRights">
    <Columns>
    <asp:TemplateField HeaderText="BaseLID" SortExpression="BaseLID" >
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList4" runat="server"
    DataSourceID="DBLanguages"
    DataTextField="LanguageUITextOrig"
    DataValueField="LanguageID" SelectedValue='<%# Bind("BaseLID") %>'>
    </asp:DropDownList>
    </EditItemTemplate>

    <ItemTemplate>
    <asp:Label ID="Label33" runat="server" Text='<%#
    GetLangText((int)Eval("BaseLID")) %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>



    Incomplete code in codebehind: How to implement InstantiateIn of
    UserFunctionRights_EditItemTemplate (ITemplate derived)?


    GridView gv = new GridView();
    gv.ID = "gv1";
    string[] strArrKN = new string[1];
    strArrKN[0] = "UserID";
    gv.DataKeyNames = strArrKN;
    gv.DataSourceID = "DBUserFunctionRights";

    TemplateField tlf_a = new TemplateField();
    tlf_a.HeaderText = "Base language";
    tlf_a.SortExpression = "BaseLID";
    tlf_a.ItemTemplate = new UserFunctionRights_ItemTemplate("BaseLID");
    tlf_a.EditItemTemplate = new UserFunctionRights_EditItemTemplate();
    gv.Columns.Add(tlf_a);
    ....

    public class UserFunctionRights_ItemTemplate : ITemplate
    {
    private string m_strVarName = "";

    public UserFunctionRights_ItemTemplate(string strVarName)
    {
    m_strVarName = strVarName;
    }

    public void InstantiateIn(Control container)
    {
    Label l = new Label();
    l.DataBinding += new EventHandler(this.BindData);
    container.Controls.Add(l);
    }

    public void BindData(object sender, EventArgs e)
    {
    Label l = (Label)sender;
    GridViewRow container = (GridViewRow)l.NamingContainer;
    l.Text =
    GetLangText((int)((DataRowView)container.DataItem)[m_strVarName]);
    }

    }

    public class UserFunctionRights_EditItemTemplate : ITemplate
    {
    private string m_strVarName = "";

    public UserFunctionRights_ItemTemplate(string strVarName)
    {
    m_strVarName = strVarName;
    }

    public void InstantiateIn(Control container)
    {
    Label l = new Label();
    l.DataBinding += new EventHandler(this.BindData);
    container.Controls.Add(l);
    }

    public void BindData(object sender, EventArgs e)
    {
    Label l = (Label)sender;
    GridViewRow container = (GridViewRow)l.NamingContainer;

    // ??? equivalent to <%# Bind > ???
    }

    }
     
    , Nov 16, 2005
    #1
    1. Advertising

  2. Guest

    I found a solution for my problem:

    gv1.RowUpdating += new GridViewUpdateEventHandler(gv1_RowUpdating);


    protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs
    e)
    {
    // value of template field has to be updated manually
    int iIdx = gvUserFunctionRights.EditIndex;
    GridViewRow row = gv1.Rows[iIdx];
    DropDownList ddl =
    (DropDownList)row.Controls[0].Controls[0];
    e.NewValues["BaseLID"] = ddl.SelectedValue;
    }
     
    , Nov 23, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arne

    one or two way Databinding

    Arne, Aug 18, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    311
    MS News \(MS ILM\)
    Aug 18, 2003
  2. =?Utf-8?B?Sm9obiBCYWlsZXk=?=

    No two-way databinding

    =?Utf-8?B?Sm9obiBCYWlsZXk=?=, Jun 9, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    641
    =?Utf-8?B?Sm9obiBCYWlsZXk=?=
    Jun 10, 2005
  3. =?Utf-8?B?dGh6ZXJv?=

    Two-way databinding to Business Object (ASP.NET 2.0)

    =?Utf-8?B?dGh6ZXJv?=, Sep 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    5,672
    =?Utf-8?B?VHJhaWwgTW9uc3Rlcg==?=
    Nov 7, 2005
  4. rjl
    Replies:
    7
    Views:
    577
    Karl Seguin
    Dec 24, 2005
  5. Prince Mathew

    URGENT-DataBinding expression in the codebehind

    Prince Mathew, Jan 26, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    149
    Prince Mathew
    Jan 26, 2005
Loading...

Share This Page