问题是在转发器中获取正确的控制/更新面板

时间:2017-09-20 20:49:19

标签: c# jquery asp.net updatepanel repeater

我遇到了在转发器内部动态更新下拉列表控件的问题。

基本上我有一个转发器,里面的转发器是2个下拉列表。一个列表在我的aspx页面中定义,另一个下拉列表在更新面板中,我希望能够根据第一个下拉列表的选择动态更新它。我认为我的问题的一部分是更新面板变得混乱,因为我有多个转发器项目?

以下是我的转发器的代码:

<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>

下面是我选择的索引更改的代码:

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)
    {

    }
}

下面是我在看多个转发器项目时所看到的截图:

enter image description here

基本上现在发生的是如果我更新第2项中的过滤器类型,它将更新第1项中的过滤器值。如果我更新第1项中的过滤器类型,它将按预期更新第1项中的过滤器值。

我想要的是能够更新item2过滤器类型,然后相应地更新第2项中的过滤器值。

任何人都对我可能遗失的内容有任何想法?

1 个答案:

答案 0 :(得分:0)

所以最终得到这个工作,我认为主要的问题是clientidmode以某种方式混淆了更新面板,所以我删除了它并使更新面板绕过两个下拉。我也没有最终需要触发器,所以我也删除了它。