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

D

dieter

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 > ???
}

}
 
D

dieter

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top