If DateRepeater and RowRepeater are like in the demo I made in the other thread, then you have them the wrong way around.
Loop over RowRepeater.Items. (You can use foreach instead of a for with an index variable. But it's a matter of taste, really.)
In each Item, find DateRepeater using FindControl. You will need to cast the result to Repeater. Then you can loop over the Items collection of that.
Use FindControl on each of these Items to find the Controls from the Template. Then get the values you need from those.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
khurj01
Member
25 Points
269 Posts
Loop through each control in a Repeater in C#
Aug 18, 2012 06:18 AM|LINK
How do I loop through each control in a repeater (highlighted below)? I need to access CallStatus and txtMeals .
TextBox box = this.DateRepeater.Items[item].FindControl("txtMeals") as TextBox;
The code above has an error "DataRepeater" not found...
<table class="DataWebControlStyle" cellspacing="3" rules="all" border="1" width="2000" style="border-collapse:collapse;white-space: nowrap"> <tr> <th>Employee</th> <asp:ObjectDataSource runat="server" ID="HeaderDataSource" TypeName="NestedRepeater.MatrixData" SelectMethod="GetHeaders"> </asp:ObjectDataSource> <asp:Repeater runat="server" ID="HeaderRepeater" DataSourceID="HeaderDataSource"> <ItemTemplate> <th class="col2" style="table-layout:inherit;"><%# ((DateTime)Container.DataItem).ToString("m") %></th> </ItemTemplate> </asp:Repeater> </tr> <asp:ObjectDataSource runat="server" ID="RowDataSource" TypeName="NestedRepeater.MatrixData" SelectMethod="GetRows"> </asp:ObjectDataSource> <asp:Repeater runat="server" ID="RowRepeater" DataSourceID="RowDataSource"> <ItemTemplate> <tr> <td align="center" style="background:steelblue; color:yellow"> <%# Eval("Name") %> </td> <asp:ObjectDataSource runat="server" ID="DateDataSource" TypeName="NestedRepeater.MatrixData" SelectMethod="GetHeaders"> </asp:ObjectDataSource> <asp:Repeater runat="server" ID="DateRepeater" DataSourceID="DateDataSource" OnItemDataBound="DateRepeater_ItemDataBound"> <ItemTemplate> <td style="background:lightyellow;"> Call Status: <asp:DropDownList runat="server" ID="CallStatus" Width="75"> <asp:ListItem Text ="OFF" /> <asp:ListItem Text ="OUT" /> </asp:DropDownList> <br /> Meals: <asp:TextBox runat="server" ID="txtMeals" Width="75"></asp:TextBox> </td> </ItemTemplate> </asp:Repeater> </tr> </ItemTemplate> </asp:Repeater> </table>shashank_meh...
Contributor
2868 Points
436 Posts
Re: Loop through each control in a Repeater in C#
Aug 18, 2012 08:13 AM|LINK
This might help you..
In OnItemDataBound Event of Repeater control "DateRepeater" write the following code:
protected void DateRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { TextBox txtMeals = (TextBox)e.Item.FindControl("txtMeals"); DropDownList drpCallStatus = (DropDownList)e.Item.FindControl("CallStatus"); }Reply me if any issues.
khurj01
Member
25 Points
269 Posts
Re: Loop through each control in a Repeater in C#
Aug 19, 2012 02:45 AM|LINK
private void SaveData() { for (int item = 0; item < this.DateRepeater.Items.Count; item++) { TextBox box = this.RowRepeater.Items[item].FindControl("txtMeals") as TextBox; string ss = box.Text; string s; if (String.IsNullOrEmpty(ss)) { s = "empty field"; } else { s = box.Text; } } }I need to loop when a button is clicked which calles a method SaveData() where I need to read each fields and save the data in a database.
superguppie
All-Star
48225 Points
8679 Posts
Re: Loop through each control in a Repeater in C#
Aug 20, 2012 08:08 AM|LINK
If DateRepeater and RowRepeater are like in the demo I made in the other thread, then you have them the wrong way around.
Loop over RowRepeater.Items. (You can use foreach instead of a for with an index variable. But it's a matter of taste, really.)
In each Item, find DateRepeater using FindControl. You will need to cast the result to Repeater. Then you can loop over the Items collection of that.
Use FindControl on each of these Items to find the Controls from the Template. Then get the values you need from those.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
khurj01
Member
25 Points
269 Posts
Re: Loop through each control in a Repeater in C#
Aug 22, 2012 04:06 AM|LINK
Could you please provide some code?
shashank_meh...
Contributor
2868 Points
436 Posts
Re: Loop through each control in a Repeater in C#
Aug 22, 2012 07:21 AM|LINK
Hi,
Check out the below code which Loops through the Repeater control items on Save Button Click.
protected void btnSave_Click(object sender, EventArgs e) { SaveData(); } private void SaveData() { foreach (RepeaterItem ri in DateRepeater.Items) { TextBox box = ri.FindControl("txtMeals") as TextBox; CheckBox bhkBox = ri.FindControl("CallStatus") as CheckBox; bool IsChecked = bhkBox.Checked; string ss = box.Text; string s; if (String.IsNullOrEmpty(ss)) { s = "empty field"; } else { s = box.Text; } } }Reply if any issues.
shashank_meh...
Contributor
2868 Points
436 Posts
Re: Loop through each control in a Repeater in C#
Aug 22, 2012 07:29 AM|LINK
Hi,
In your second post, the code you have written is correct, but in the for loop condition your are using DateRepeater Control.
But in the very next line to retrieve the record you are using RowRepeater control.
this.RowRepeater.Items[item].FindControl("txtMeals") as TextBox;If you make the second line of your code like the below one it should work
this.DateRepeater.Items[item].FindControl("txtMeals") as TextBox;