locked
Loop through each control in a Repeater in C# RRS feed

  • Question

  • User-1991712009 posted

    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> 
    

    Saturday, August 18, 2012 2:18 AM

Answers

  • User1455999623 posted

    Hi,

    In your second post, the code you have written is correct, but in the for loop condition your are using DateRepeater Control.

    this.DateRepeater.Items.Count

    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; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 22, 2012 3:29 AM

All replies

  • User1455999623 posted

    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.

    Saturday, August 18, 2012 4:13 AM
  • User-1991712009 posted
            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.

     

    Saturday, August 18, 2012 10:45 PM
  • User16212438 posted

    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. 

    Monday, August 20, 2012 4:08 AM
  • User-1991712009 posted

    Could you please provide some code?

    Wednesday, August 22, 2012 12:06 AM
  • User1455999623 posted

    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.

    Wednesday, August 22, 2012 3:21 AM
  • User1455999623 posted

    Hi,

    In your second post, the code you have written is correct, but in the for loop condition your are using DateRepeater Control.

    this.DateRepeater.Items.Count

    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; 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 22, 2012 3:29 AM