1

I recently switched a ComboBox in my repeater to a SuggestComboBox (found: here) because it is a contains search rather than a starts-with search. Previously, I was using NamingContainer of that ComboBox to find nearby elements. My new SuggestComboBox does not have this value but in my research on this problem, it looks like all children of a repeater should have this already?

"the NamingContainer property is available in code to any instance of that class or of a derived class." (found: here)

What am I missing?

Here's my repeater:

<asp:Repeater ID="repHW" runat="server" OnItemCommand="rep_ItemCommand">
        <HeaderTemplate>
            <table style="width:100%; padding-bottom:10px" id="HWtable">
                <tr style="font-weight: bold"><td>Product</td><td>Part Number</td><td>Cost</td><td>Unit Price</td><td>Quantity</td><td>Price</td><td>Delete</td></tr>
        </HeaderTemplate>
        <ItemTemplate>
                <tr>
                    <asp:HiddenField ID="Category" Value="Hardware" runat="server"/>
                    <td><asp:Label ID="Product" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.Name") %>' /></td> <!--TODO: make this clickable to edit -->
                    <td><asp:Label ID="PartNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.PartNumber") %>' /></td>
                    <td><asp:Label ID="PartCost" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.Cost") %>' /></td>
                    <td><asp:Label ID="UnitPrice" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product.Price") %>' /></td>
                    <td><asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>' /></td>
                    <td><asp:Label ID="Price" runat="server" Text='<%# Eval("Total") %>' /></td>
                    <td><asp:Button class="btn btn-danger" ID="DeleteHardware" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Container.ItemIndex %>'/></td>
                </tr>
        </ItemTemplate>
        <FooterTemplate>
                <tr>
                    <asp:HiddenField ID="AddCategory" Value="Hardware" runat="server"/>
                    <td><SuggestComboBox runat="server" ID="AddProduct" AutoCompleteMode="SuggestAppend" AutoPostBack="true" OnSelectedIndexChanged="ProductSelected" OnDataBinding="LoadHardwareProducts"/></td>
                    <td><asp:TextBox runat="server" ID="AddPartNumber" ClientIDMode="static"/></td>
                    <td><asp:TextBox runat="server" ID="AddPartCost" ClientIDMode="static"/></td>
                    <td><asp:TextBox runat="server" ID="AddUnitPrice" ClientIDMode="static"/></td>
                    <td><asp:TextBox runat="server" ID="AddQuantity" ClientIDMode="static"/></td>
                    <td><asp:Button class="btn btn-success" ID="AddHardware" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Container.ItemIndex %>' onClientClick="return EmptyFieldCheck('Hardware');"/></td>
                </tr>
            </table>    
        </FooterTemplate>
    </asp:Repeater>

and here's my function I am trying to access the other elements in:

protected void ProductSelected(Object source, EventArgs e)
    {
        SuggestComboBox temp = (SuggestComboBox)source;

        List<Product> results = session.Query<Product>()
            .Where(x => x.Name == temp.Text)
            .ToList();


        if(results.Count > 0)
        {
            Product p = results[0];

            var repParent = temp.NamingContainer; //this broke

            TextBox partNum = (TextBox)repParent.FindControl("AddPartNumber");
            TextBox partCost = (TextBox)repParent.FindControl("AddPartCost");
            TextBox unitPrice = (TextBox)repParent.FindControl("AddUnitPrice");
            TextBox quantity = (TextBox)repParent.FindControl("AddQuantity");

            partNum.Text        = p.PartNumber;
            partCost.Text       = p.Cost.ToString();
            unitPrice.Text      = p.Price.ToString();
            quantity.Text       = p.DefaultQuantity.ToString();

        }
    }
  • I think you need to elaborate on the error/problem that you're experiencing. Are you saying that the SuggestComboBox class doesn't have a NamingContainer property and consequently the code doesn't compile? Or are you experiencing a problem at runtime? What does the SuggestComboBox class inherit from? – Dr. Wily's Apprentice Mar 16 '16 at 21:54
  • @Dr.Wily'sApprentice Correct it will not compile, but it inherits from the ComboBox class that could use the NamingContainer. That's why I liked this solution in the first place, it should have been drop-in... – Jordan Wayne Crabb Mar 16 '16 at 21:56
  • Weird. It inherits from ComboBox, then it should have that property. Are you getting a runtime error, then, or compile-time? If runtime, well... the property is virtual, so theoretically the SuggestComboBox class could be overriding the implementation for some reason. Not sure why it would. That doesn't really help you with your problem... Ok, SuggestComboBox should at least have a Parent property, right? Try getting its parent and then using the naming container from that. Or, just use the FindControl method directly off of the SuggestComboBox or its parent. – Dr. Wily's Apprentice Mar 16 '16 at 22:00
  • Hmm. It may inherit from a class that is named ComboBox, but is that actually the same ComboBox class that you removed from the original implementation, or is it a different ComboBox class that happens to have the same name? BTW there isn't a ComboBox web control in the .NET framework; there's a DropDownList web control, though. There is a ComboBox WinForms control, though, just in case that's causing some confusion. – Dr. Wily's Apprentice Mar 16 '16 at 22:11

0 Answers0