更新面板并下拉选定的索引更改问题

本文关键字:索引 问题 更新 | 更新日期: 2023-09-27 18:24:58

我的页面上有两个下拉类别和子类别。此外,我在同一页上有一个htmleditor。下面是我的aspx

<div class="clearfx">
    </div>
     <div>
         <label>
             Description :
                </label>
                 <div style="padding-left: 10px; margin-left: 146px;">
                    <cc:HtmlEditor ID="Editor" runat="server" Height="600px" Width="850px" EnableViewState="true" />
                            </div>
                        </div>
                        <div class="clearfx">
                        </div>
                        <div>
                            <asp:UpdatePanel runat="server" ID="up" UpdateMode="Always" ChildrenAsTriggers="true">
                                <ContentTemplate>
                                    <label>Category : </label>
                                    <asp:DropDownList runat="server" ID="Categories" Width="200px" AutoPostBack="True"
                                        OnSelectedIndexChanged="CategoriesSelectedIndexChanged">
                                    </asp:DropDownList>
                                   <asp:RequiredFieldValidator ID="rfvCountry" runat="server" ErrorMessage="Choose a category."
                                        ControlToValidate="Categories" Display="None" EnableTheming="False" EnableViewState="False"
                                        InitialValue="-1" SetFocusOnError="True" ValidationGroup="grpReg"></asp:RequiredFieldValidator>
                                    <asp:ValidatorCalloutExtender ID="rfvCountry_ValidatorCalloutExtender" runat="server"
                                        Enabled="True" TargetControlID="rfvCountry">
                                    </asp:ValidatorCalloutExtender>
                                     <asp:ImageButton runat="server" ID="Refresh" ImageAlign="AbsMiddle" 
                                        ImageUrl="~/cdn/images/refresh.png" onclick="RefreshClick"/> 
                                         <span class="helptext">(click on refresh image, incase subcategories do not load.)</span>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </div>
                        <div class="clearfx">
                        </div>
<div>
                            <label>
                                Sub Category :
                            </label>
                            <asp:DropDownList runat="server" ID="SubCategories"  Width="200px">
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Choose a sub category."
                                ControlToValidate="SubCategories" Display="None" EnableTheming="False" EnableViewState="False"
                                InitialValue="-1" SetFocusOnError="True" ValidationGroup="grpReg"></asp:RequiredFieldValidator>
                            <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" runat="server" Enabled="True"
                                TargetControlID="RequiredFieldValidator2">
                            </asp:ValidatorCalloutExtender>
                        </div>

我正在做的是,在类别下拉列表的选定索引更改上,我正在填充子类别。为了抑制回帖,我使用了更新面板,但我的问题是,所选的索引更改被触发,但下拉子类别没有被绑定。当我删除更新面板时,它可以正常工作,但我的html编辑器失去了它的值,甚至有自己的和Page enableviewstate=true。请帮帮我。

我的Codebehing方法是:下拉选择的索引更改事件:

protected void CategoriesSelectedIndexChanged(object sender, EventArgs e)
        {
            if (Categories.SelectedIndex > 0)
            {
                BindSubCategory(Common.ParseInt(Categories.SelectedItem.Value));
            }
            else
            {
                SubCategories.SelectedIndex = 0;
                SubCategories.Items.Clear();
            }
        } 

页面加载事件:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                BindCategories();
            }
        }
 private void BindCategories()
        {
            List<Category> cat = _categoryRepository.GetAll().ToList();
            Common.BindDropdown(cat, Categories); // static method
        }
 public static class Common
    {
 /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="ddl"></param>
        public static void BindDropdown(IList list, DropDownList ddl)
        {
            if (list.Count>0)
            {

                    ddl.DataSource = list;
                    ddl.DataTextField = "Name";
                    ddl.DataValueField = "Id";
                    ddl.DataBind();
                    ddl.Items.Insert(0, new ListItem("---Select---", "-1"));
            }
        }
}

更新面板并下拉选定的索引更改问题

您需要将子类别放在另一个UpdatePanel中,并添加适当的触发器(将第二个面板设置为在dexchanged事件中选择的Categories下拉菜单上触发)。

 <Triggers>
            <asp:AsyncPostBackTrigger ControlId="Categories" 
              EventName="SelectedIndexChanged" />
  </Triggers>

此处采样

试试这个:

<div class="clearfx"> 
</div> 
<div> 
    <label> Description : </label> 
    <div style="padding-left: 10px; margin-left: 146px;"> 
        <cc:HtmlEditor ID="Editor" runat="server" Height="600px" Width="850px" EnableViewState="true" /> 
    </div> 
</div> 
<div class="clearfx"> 
</div> 

<asp:UpdatePanel runat="server" ID="up" UpdateMode="Always" ChildrenAsTriggers="true"> 
    <ContentTemplate> 
        <div>
            <label>Category : </label> 
            <asp:DropDownList runat="server" ID="Categories" Width="200px" AutoPostBack="True" 
                OnSelectedIndexChanged="CategoriesSelectedIndexChanged"> 
            </asp:DropDownList> 
            <asp:RequiredFieldValidator ID="rfvCountry" runat="server" ErrorMessage="Choose a category." 
                ControlToValidate="Categories" Display="None" EnableTheming="False" EnableViewState="False" 
                InitialValue="-1" SetFocusOnError="True" ValidationGroup="grpReg"></asp:RequiredFieldValidator> 
            <asp:ValidatorCalloutExtender ID="rfvCountry_ValidatorCalloutExtender" runat="server" 
                Enabled="True" TargetControlID="rfvCountry"> 
            </asp:ValidatorCalloutExtender> 
            <asp:ImageButton runat="server" ID="Refresh" ImageAlign="AbsMiddle"  
                ImageUrl="~/cdn/images/refresh.png" onclick="RefreshClick"/>  
            <span class="helptext">(click on refresh image, incase subcategories do not load.)</span> 
            <div class="clearfx"> 
            </div> 
            <div> 
                <label> Sub Category : </label> 
                <asp:DropDownList runat="server" ID="SubCategories"  Width="200px"> 
                </asp:DropDownList> 
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Choose a sub category." 
                    ControlToValidate="SubCategories" Display="None" EnableTheming="False" EnableViewState="False" 
                    InitialValue="-1" SetFocusOnError="True" ValidationGroup="grpReg"></asp:RequiredFieldValidator> 
                <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" runat="server" Enabled="True" 
                    TargetControlID="RequiredFieldValidator2"> 
                </asp:ValidatorCalloutExtender> 
            </div>
        </div>  
    </ContentTemplate> 
</asp:UpdatePanel>