468,532 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

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

Two-way databinding (as described in
http://dotnetjunkies.com/QuickStartv...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 20 '05 #1
1 2665
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 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jamey Cribbs | last post: by
5 posts views Thread by surrealtrauma | last post: by
6 posts views Thread by Laura K | last post: by
5 posts views Thread by nelly0 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.