Search code examples
c#jqueryasp.netupdatepanelrepeater

Issue grabbing correct control/update panel in repeater


I'm having issues with dynamically updating a drop down list control when it is inside a repeater.

Basically I have a repeater and inside that repeater are 2 drop down lists. The one list is defined in my aspx page, the other drop down list is inside an update panel where I want to be able to have it dynamically update based on the selection of the first drop down list. I think part of my problem is the updatepanel is getting confused because I have more than one repeater item?

Here is the code for my repeater:

<asp:Repeater ID="billingTemplate" runat="server" OnItemDataBound="template_ItemDataBound">
    <ItemTemplate>
        <tr style="font-size: 100%" runat="server">
            <td colspan="4" style="width: 100%; vertical-align: top">
                <div class="panel panel-default panel-billing">
                    <asp:Panel CssClass="row panel-heading panel-heading-billing text-left" ID="headingBilling" ClientIDMode="Static" runat="server">
                        <div class="col-xs-1">
                            <input type="hidden" id="templateUK" runat="server" value='<%#Eval("templateUK")%>' />
                            <a href="#collapseBilling" id="BillingPanelBtn" clientidmode="Static" class="btn btn-block btn-group-xs panel-button glyphicon glyphicon-chevron-down" data-toggle="collapse" runat="server"></a>
                        </div>
                        <div class="col-sm-3">
                            <label for="ddlInvFilterType" class="col-sm-4 control-label text-right labelCls testclass">Filter Type:</label>
                            <div class="col-sm-8">
                                <asp:DropDownList runat="server" ID="ddlInvFilterType" ClientIDMode="Static" placeholder="Choose Filter Type" CssClass="form-control smallSize FilterType" AutoPostBack="true" OnSelectedIndexChanged="ddlFilterType_SelectedIndexChanged">
                                    <asp:ListItem Value="">- None -</asp:ListItem>
                                    <asp:ListItem Value="RevType1">Revenue Type 1</asp:ListItem>
                                    <asp:ListItem Value="RevType2">Revenue Type 2</asp:ListItem>
                                    <asp:ListItem Value="RevType3">Revenue Type 3</asp:ListItem>
                                    <asp:ListItem Value="ServiceTeams">Service Team</asp:ListItem>
                                </asp:DropDownList>
                            </div>
                        </div>
                        <asp:UpdatePanel ID="InvFilterValuePanel" ClientIDMode="Static" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
                            <ContentTemplate>
                                <div class="col-sm-3">
                                    <label for="ddlInvFilterValue" class="col-sm-4 control-label text-right labelCls">Filter Value:</label>
                                        <div class="col-sm-8">
                                            <asp:DropDownList runat="server" ID="ddlInvFilterValue" ClientIDMode="Static" placeholder="Choose Filter Value" CssClass="col-sm-6 form-control smallSize">
                                                <asp:ListItem Value="">- None -</asp:ListItem>
                                            </asp:DropDownList>
                                        </div>
                                </div>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="ddlInvFilterType" EventName="SelectedIndexChanged" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </asp:Panel>
                    <asp:Panel CssClass="panel-collapse collapse" ID="collapseBilling" ClientIDMode="Static" runat="server">
                        <div class="panel-body">
                            <table class="table table-condensed table-bordered" style="margin: 0; padding: 0; border-top: none; border-bottom: none; border-left: thin; border-right: thin">
                                <tbody>                                                                
                                    <%-- other controls --%>
                                </tbody>
                            </table>
                        </div>
                    </asp:Panel>
                </div>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

Heres the code for my selected index change:

protected void ddlFilterType_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        DropDownList ddl = (DropDownList)sender;

        string ddlClass = ddl.CssClass;
        string[] classes = ddlClass.Split(' ');
        string repeaterClass = classes[classes.Length - 1];
        string repeaterID = "0";

        string appendStr = "";

        if (repeaterClass.Contains("templateID"))
        {
            repeaterID = repeaterClass.Substring(repeaterClass.Length - 1);
            appendStr = "_" + repeaterID;
        }            

        foreach (RepeaterItem item in billingTemplate.Items)
        {
            HtmlInputHidden hidden = item.FindControl("headingBilling").FindControl("templateUK") as HtmlInputHidden;

            if (hidden.Value == repeaterID)
            {
                DropDownList d1 = item.FindControl("headingBilling").FindControl("ddlInvFilterType") as DropDownList;
                DropDownList d2 = item.FindControl("headingBilling").FindControl("ddlInvFilterValue") as DropDownList;

            if (d1.SelectedValue.Length > 0)
            {
                d2.Items.Clear();
                d2.Items.Add(new ListItem(" - None - ", ""));
                    switch (d1.SelectedValue)
                    {
                        case "ServiceTeams":
                            foreach (var pair in serviceTeamsController.GetAllServiceTeamsLOVs())
                            {
                                if (!String.IsNullOrWhiteSpace(pair.Value))
                                    d2.Items.Add(new ListItem(pair.Value, pair.Key));
                            }
                            break;
                        default:
                            foreach (var pair in masterController.GetMasterLOVs(filterTypeDict[d1.SelectedValue]))
                            {
                                if (!String.IsNullOrWhiteSpace(pair.Value))
                                {
                                    d2.Items.Add(new ListItem(pair.Value, pair.Key));
                                }
                            }
                            break;                                
                        }
                    }
                    else
                    {
                        d2.Items.Clear();
                        d2.Items.Add(new ListItem(" - None - ", ""));
                    }                   
            }                
        }
    }
    catch (Exception ex)
    {

    }
}

Heres a screenshot of what I'm looking at when theres more than one repeater item:

enter image description here

Basically whats happening now is if I update the filter type in item2 it will update the filter value in item 1. If I update the filter type in item1 it will update the filter value in item 1 as expected.

What I want is to be able to update item2 filter type and then the filter value in item 2 is updated accordingly.

Anyone have any ideas on what I could be missing?


Solution

  • So ended up getting this to work, I think the main issue was the clientidmode was confusing the update panel somehow, so I removed that and made the update panel go around both drop downs. I also didnt end up needing the trigger so I removed that as well.