更新面板和下拉列表选择的索引更改问题

时间:2012-02-18 13:36:15

标签: c# asp.net data-binding drop-down-menu

我的页面上有两个下拉类别和子类别。我也在同一页面上有一个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编辑器失去了它的价值,即使有自己的和页面enableviewstate = true。请帮帮我。

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

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"));

            }
        }
}

2 个答案:

答案 0 :(得分:0)

您需要将子类别放在另一个UpdatePanel中并添加正确的触发器(将第二个面板设置为在类别下拉列表中触发selectedindexchanged事件)。

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

sample here

答案 1 :(得分:-2)

试试这个:

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