1

What I am trying to do is access a column (which has the header title "Add to Shopping List") inside a repeater (areaRepeater) that is inside another repeater (locationRepeater). I want to hide it if a checkbox is checked. However, despite how things are setup, the column is never hidden. I'm not sure what I'm missing here...or maybe I'm going about it the wrong way?

Here is the piece of code that I am trying to use to hide the column in the areaRepeater table. I can hide the submitBtn button successfully, but that button is not inside a repeater.

                    Sitecore.Data.Fields.CheckboxField checkBox = ProductGroup.Fields["Shopping Disabled"];
                    if (checkBox.Checked)
                    {
                        submitBtn.Visible = false;

                        Repeater rpt1 = (Repeater)FindControl("locationRepeater");
                        Response.Write("a ");
                        foreach (RepeaterItem rep in rpt1.Items)
                        {
                            Response.Write("1 ");
                            Repeater areaRepeater = (Repeater)rep.FindControl("areaRepeater");

                            foreach (RepeaterItem areaRep in areaRepeater.Items)
                            {
                                Response.Write("2 ");
                                if (showField() == false)
                                {
                                    Label lbl1 = (Label)areaRep.FindControl("litCol");
                                    CheckBox check = (CheckBox)areaRep.FindControl("LineQuantity");
                                    lbl1.Visible = false;
                                    check.Visible = false;
                                }


                            }
                        }
                    }

This is the designer code for both repeaters. I tried setting the visibility of the label for the header and the checkbox with a function called showField() but it is never called, even though it does return the correct bool value:

 <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">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="listCol" 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 showField() function:

protected bool showField()
{
    bool retVal = true;
    Item CurrentItem = Sitecore.Context.Item;
    Item HomeItem = ScHelper.FindAncestor(CurrentItem, "Market");

    if (HomeItem != null)
    {
        Item ProductGroup = HomeItem.Axes.SelectSingleItem(@"child::*[@@templatename='MarketOfficeBuildigProductMap']");

        if (ProductGroup != null)
        {
            Sitecore.Data.Fields.CheckboxField checkBox = ProductGroup.Fields["Shopping Disabled"];//curently returns true
            ShoppingDisabled.Value = checkBox.Checked.ToString();
            if (checkBox.Checked == true)
            {
                retVal = false;
            }
        }
    }
    return retVal;
}

1 Answer 1

0

I think you could approach this from a different angle which could make your code a lot simpler.

First off try not to use the OnDataBound event. I prefer to use the DataBinding event for the specific controls as it localizes the code better and you never have search for controls. I am not exactly sure how your code is working but take a look at how I would implement the two controls you are trying to hide to use the DataBinding event.

e.g. for the Checkbox:

<asp:CheckBox id="LineQuantity" runat="server" OnDataBinding="LineQuantity_DataBinding" />

protected void LineQuantity_DataBinding(object sender, System.EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    chk.Checked = (bool)(Eval("SomeField"));  // Note you can use Eval here...
    chk.Visible = showField();
}

This should help you localize the problem at least. I typically use this method of doing any custom work while databinding. The DataBound event is not great because it happens after it is all done. The OnDataBinding event lets you inject whatever type of logic you want at the time the databinding is occurring.

2
  • Ok, thanks! I will give this a try as soon as I can. Commented Jul 2, 2014 at 17:33
  • All right, I tried that, but it did not work for me. I have everything as shown in your example, but I replaced "SomeField" with "Shopping Disabled," which is the field in Sitecore that determines if the column is hidden or not.... Commented Jul 2, 2014 at 18:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.