3

I have a question about how to perform a two-way databinding in an ASP.NET control (in this example a repeater control). The technology is Web Forms. My question is, where can I read the user's data after a postback? (See MyButton_Click below).

Markup:

<asp:Repeater runat="server" ID="MyRepeater"> <ItemTemplate> <asp:Label runat="server">Name:&nbsp;</asp:Label><asp:TextBox ID="MyTextBox" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox><br/> </ItemTemplate> </asp:Repeater> <asp:Button runat="server" ID="MyButton" Text="Hit me" OnClick="MyButton_Click" /> 

Interface:

interface.jpg

Code behind:

protected void Page_Load(object sender, EventArgs e) { MyRepeater.DataSource = ( from d in Data.GetData() select new { Name = d }).ToList(); MyRepeater.DataBind(); } protected void MyButton_Click(object sender, EventArgs e) { // Ok now, where is the user's changed data...? } 
1
  • Noone knows? Am I thinking totally wrong about two way bindings? Somehow it must be possible to get the data from the user... Commented Feb 7, 2014 at 13:56

1 Answer 1

2

Ok, I've found that it is not possible to do two way binding in this way. I though of two way binding as a way to have the assigned data source to be updated automatically, but that seems only possible when using the DataSource web controls, such as SqlDataSource, XmlDataSource, etc. But then it seems not possible to customize the processing of the data.

A solution is to loop through the repeater items and simply read the control explicitly. So the answer to the question and how to get the user's data is:

protected void MyButton_Click(object sender, EventArgs e) foreach (RepeaterItem repeaterItem in MyRepeater.Items) { TextBox myTextBox = (TextBox)repeaterItem.FindControl("MyTextBox"); var usersData = myTextBox.Text; // Here it is! } } 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm in the same boat, but those all come back empty for me :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.