473,396 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2799
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jamey Cribbs | last post by:
At the risk of calling my manhood into question, I humbly submit the following little diddy (which is a blatant rip-off of a heart wrenching melody of the '70s by that international superstar, Mary...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
8
by: Mary Jane Pronio | last post by:
Any information would be greatly appreciated~~!! Thanks!
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
6
by: Laura K | last post by:
This is probably a simple question but I want to make sure I am doing it right. I have a spoc with two select statements which results in two tables. Very Basic ...
3
by: Simon | last post by:
Dear reader, For matching of two fields it is required that the fields has the same content. But now I have the situation that two text fields has not precisely the same content but there...
2
by: slinky | last post by:
Anyone know how to calculate the difference between two times displayed in two textboxes? I'm starting out with two textboxes: "txtCallTimeBegins" & "txtCallTimeEnds" this yielded: 6/7/2007...
5
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.