Search code examples
c#checkboxrepeater

Unable to locate checkbox control inside repeater


I have a function that is meant to find the checkbox associated with each product item in a repeater. If the checkbox is checked, then the int variable qty is set to 1. The problem is, for some reason, the function cannot locate the checkbox controls:

This is the setup for the repeaters (there is one inside the other):

 <asp:Repeater ID="locationRepeater" runat="server" OnItemDataBound="SetInner">   
    <ItemTemplate>
        <div class="LocationName">
             <%# Eval("SecOpen") %><%# Eval("LocationName")%> <%# Eval("SecClose") %>
        </div>
        <asp:Repeater ID="areaRepeater" runat="server">  
         <HeaderTemplate>
            <div class="headerRow">
                    <div class="header">
                        <div class="thumb"><p></p></div>
                        <div class="headerField name"><p class="hField">Product</p></div>
                        <div class="headerField sku"><p class="hField">GOJO SKU</p></div>
                        <div class="headerField size"><p class="hField">Size</p></div>
                        <div class="headerField case"><p class="hField">Case Pack</p></div>
                        <div class="headerField use"><p class="hField">Use With</p></div>
                        <div id="shoppingHeader" class="headerField qty" runat="server"><p class="headerfield qty hField"><asp:Label id="list" runat="server" visible='<%# (showField()) %>' Text="Add To Shopping List" /> </p></div>

                    </div>
             </div>
        </HeaderTemplate>      
            <ItemTemplate>
                <asp:placeholder id="LocationAreaHeader" runat="server" visible='<%# (Eval("AreaName").ToString().Length == 0  ? false : true) %>' ><h3> <%# Eval("AreaName") %></h3></asp:placeholder>

                    <asp:placeholder id="ProductTable" runat="server" visible='<%# (Eval("ProductName").ToString().Length == 0  ? false : true) %>' >

                       <div class="table">
                           <div class="row">
                               <div class="thumb"><%# Eval("Charm") %></div>
                                <div class="field name"><p class="pField"> <%# Eval("ThumbOpen") %><%# Eval("ProductName") %><%# Eval("ThumbClose") %></p> </div>
                                <div class="field sku"><p class="pField"> <%# Eval("Sku") %> </p></div>
                                <div class="field size"><p class="pField"> <%# Eval("Size") %></p></div>
                                <div class="field case"><p class="pField"> <%# Eval("CasePack") %> </p></div>
                                <div class="field use"><p class="pField"> <%# Eval("UseWith") %> </p></div>
                                <div id="shopping" class="field qty" runat="server"><p class="pField"> 
                                   <asp:CheckBox Visible='<%# (showField()) %>' ID="LineQuantity" runat="server" /></p></div>
                            </div>
                        </div>
                           <asp:Label id="productID" text='<%# Eval("productID") %>' visible="false" runat="server" />
                    </asp:placeholder>
               <!-- Stored values -->

               <asp:Label id="SkuID" runat="server" text='<%# Eval("SkuID") %>' visible="true" />
               <asp:Label id="masterSku" runat="server" text='<%# Eval("masterSku") %>' visible="false" />
               <asp:Label id="masterName" runat="server" text='<%# Eval("masterName" ) %>' visible="false" />

             <asp:Label ID="test" visible="false" runat="server" text='<%# Eval("AreaID") %>' />

            </ItemTemplate>
        </asp:Repeater>

        <asp:Label ID="refID" visible="false" runat="server" text='<%# Eval("LocationID") %>' />
    </ItemTemplate>
</asp:Repeater>

This is the function:

    protected bool checkQtys(ref int ItemCnt)
    {
        Repeater locationRepeater = (Repeater)FindControl("locationRepeater");

        bool validQtys = true;
        string productID = "";
        int qty;
        qtyErrorMsg.Text = "";
        qtyErrorMsgTop.Text = "";
        foreach (RepeaterItem repItem in locationRepeater.Items)
        {
            if (repItem != null)
            {
                Repeater areaRepeater = (Repeater)repItem.FindControl("areaRepeater");
                if (areaRepeater != null)
                {
                    foreach (RepeaterItem skuItm in areaRepeater.Items)
                    {

                        if (skuItm != null)
                        {
                            Label SkuID = (Label)skuItm.FindControl("SkuID");
                            Label qtyID = (Label)skuItm.FindControl("qtyID");

                            PlaceHolder inner = (PlaceHolder)skuItm.FindControl("ProductTable");

                            if (inner != null)
                            {
                                foreach (Control ct in inner.Controls)
                                {
                                    Response.Write("ct: " + ct.ToString() + "<br />");//This finds all controls except the checkboxes
                                    if (ct is CheckBox)
                                    {
                                        CheckBox lineQty = (CheckBox)ct;

                                        Label prodID = (Label)inner.FindControl("productID");
                                        if (lineQty.Checked)
                                        {
                                            productID = prodID.Text;
                                            qty = 1;

                                            ItemCnt++;
                                        }

                                    }
                                }

                            }
                        }

                    }
                }

            }
        }
        return validQtys;
    }

Is a CheckBox somehow not a control, or am I searching for it in the wrong manner?


Solution

  • By looking at your code, I am not sure why you are repeating through the control collection of placeholder. You can directly find the control using the following code

    CheckBox checkBox = skuItm.FindControl("LineQuantity") as CheckBox;
    

    Since repeater item "skuItem" is the immediate parent naming container for the CheckBox, with this way you should be able to find the checkbox