Question

I have a nested repeaters in a form and bind them to tables in a dataset. I can find the controls by name and read and set values in the Itemdatabound event, However I can't find the control by the same name in a button click event. I can find the repeater and the item.

 <asp:Repeater ID="rptrDivision" OnItemDataBound="rptrDivision_ItemDataBound" runat="server">
               <itemtemplate>
                <div class="fluid">
                  <div class="widget grid12">
                    <div class="whead"><div class="grid2"><h4 class="fieldDivision" >
                        <h4 class="fieldDivision" ><asp:Label ID="lblDivName" runat="server" Visible="True" ><%# DataBinder.Eval(Container.DataItem, "LocationName")%></asp:Label></h4></div>
                        <div class="grid10">
                          <div class="grid4 divAdmin" ><label><strong>Division Administrator: </strong> </label></div>
                        <div class="grid8 on_off">
                            <div class="floatL ml10"><asp:CheckBox ID="cbxDivMgr" name="chbox" runat="server" /></div>
                        </div>
                    </div></div>
                    <asp:Repeater ID="rptrCamera" OnItemDataBound="rptrCamera_ItemDataBound" runat="server" datasource='<%# Container.DataItem.Row.GetChildRows("CameraJoin")%>'>
                          <HeaderTemplate>
                              <div class="body">
                          </HeaderTemplate>
                          <itemtemplate>
                                 <div class="formRow">
                                    <div class="grid2">
                                        <asp:Label ID="CameraName" runat="server" ><strong><%# Container.DataItem("Name")%></strong></asp:Label> </div>
                                     <div class="grid10" >
                                         <asp:RadioButtonList ID="rblRoles" runat="server" CssClass="mr10" RepeatColumns="5" CellPadding="2" CellSpacing="2">
                                             <asp:ListItem Value="role1" >Role1</asp:ListItem>
                                             <asp:ListItem Value="role2">Role2  </asp:ListItem>
                                             <asp:ListItem Value="role3" >Role3 </asp:ListItem>
                                             <asp:ListItem Value="Role4">role4 </asp:ListItem>
                                             <asp:ListItem Value="none" Selected="True">N/A </asp:ListItem>
                                         </asp:RadioButtonList>

                                    </div>
                                  </div>
                              </itemtemplate>
                          <FooterTemplate>
                               </div>
                          </FooterTemplate>
                      </asp:Repeater>  
                  </itemtemplate>

        </asp:Repeater>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"   CssClass="buttonM bBlue" OnClientClick="btnSubmit_Click" /> <asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="buttonM bBlue" OnClientClick="btnCancel_Click"/>

The submit button is outside the repeater but in the same form

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

For Each Item As RepeaterItem In rptrDivision.Items
            If (Item.ItemType = ListItemType.Item) Or (Item.ItemType = ListItemType.AlternatingItem) Then
                DivLabel = CType(Item.FindControl("lblDivName"), Label)
                DivName = DivLabel.Text
                DivMgr = CType(Item.FindControl("cbxDivMgr"), CheckBox)
          '''do some logic
       End If
    Next
   End Sub

I've looked in the debugger and item has the correct number of controls so I know I am in the right place, but the findcontrol in the repeater item can't locate the control by name. It always comes back as null.

Every example I can find from here to MSDN forums to asp.net to bytes.com show looping through the items in the repeater and calling the find control the same way. I can't figure out what I am doing wrong.

Edit: SO here is the kicker just make sure I'm not crazy I did a loop through the controlscollection on the repeater item and on the second control it comes back with the ID "lblDivName" yet if I call the item.findControl("lblDivName") it returns a null value.

Was it helpful?

Solution

Control events (like a Button's click event) occur before DataBound events (see ASP.NET Page Life Cycle on MSDN for more information). Thus, your Repeater is not DataBound yet, so its databound controls have not been initialized (they are null).

You have some options.

Preferably, you would use the Repeater's databound event to work with it's data items - since this is when you know for sure that they are initialized and populated with values.

However, you could just call "DataBind" prior to doing the processing in your Click event:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    rptrDivision.DataSource = yourDataSource
    rptrDivision.DataBind()

    For Each Item As RepeaterItem In rptrDivision.Items
    ' The rest of your code                
End Sub

This should ensure that your controls have values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top