更改DropdownList的选择会触发CheckBoxList的SelectedIndexChanged事件

时间:2012-06-27 08:30:09

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

基本上我想为Grid视图实现一个过滤选项,我有一个包含列名的下拉列表和一个包含该列的可用过滤值的checkboxlist。每次我选择不同的列时,我都必须将该列的过滤值加载到复选框列表中。 我遇到的问题是,当我更改下拉列表中的列时,将触发核对表的事件并且应用程序崩溃。

我的控件定义如下:

<div class="LeftAligned">  
     <asp:Label ID="FilterLabel" runat="server" Text="Filter by:" />  
     <asp:DropDownList runat="server" ID="FilterReviewsDropDownList" AutoPostBack="true" OnSelectedIndexChanged="FilterReviewsDropDownList_SelectedIndexChanged" />  
     <asp:ImageButton ID="FilterReviewsButton" runat="server" ImageUrl="~/images/filter.png" AlternateText="VALUE" CssClass="filter_button" OnClick="FilterReviewsButton_Click" />  
     <div onmouseout="javascript:bMouseOver=false;" onmouseover="javascript:bMouseOver=true;" class="filter_div">  
           <asp:CheckBoxList AutoPostBack="true" ID="FilterReviewsCheckBoxList" ClientIDMode="Static" runat="server" CssClass="filter_checklist collapsed"   
            OnSelectedIndexChanged="FilterReviewsCheckBoxList_Selected">  
           </asp:CheckBoxList>  
     </div>  
     <%--asp:Button runat="server" ID="ApplyFilterButton" Text="Apply Filter" OnClick="ApplyFilterButton_Click"/>  
     <asp:Button runat="server" ID="ClearFilterButton" Text="Clear Filter" OnClick="ClearFilterButton_Click"/--%>  
</div>

在CodeBehind文件中,我有以下代码:

protected void FilterReviewsButton_Click(object sender, EventArgs e)
{
    FilterReviewsCheckBoxList.CssClass = "filter_checklist";
}

protected void FilterReviewsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    LoadFilterCheckboxes(FilterReviewsDropDownList.SelectedIndex);
}

private void LoadFilterCheckboxes(int iColumn)
{
    SortedSet<string> oItems = new SortedSet<string>();

    for (int i = 0; i < ReviewsGridView.Rows.Count; i++)
    {
        IEnumerable<Label> oLabels = ReviewsGridView.Rows[i].Cells[iColumn].Controls.OfType<Label>();
        string sValue = "";
        if (oLabels != null && oLabels.Count() > 0)
        {
            sValue = oLabels.First().Text;
        }
        else
        {
            sValue = ReviewsGridView.Rows[i].Cells[iColumn].Text;
        }
        if (!oItems.Contains(sValue))
            oItems.Add(sValue);
    }

    FilterReviewsCheckBoxList.Items.Clear();
    FilterReviewsCheckBoxList.Items.Add("All");
    FilterReviewsCheckBoxList.Items[0].Selected = true;
    foreach (string sItem in oItems)
    {
        FilterReviewsCheckBoxList.Items.Add(sItem);
        FilterReviewsCheckBoxList.Items[FilterReviewsCheckBoxList.Items.Count - 1].Selected = true;
    }
}

protected void FilterReviewsCheckBoxList_Selected(object sender, EventArgs e)
{
    string sResult = Request.Form["__EVENTTARGET"];
    if (string.IsNullOrEmpty(sResult))
    {
        FilterReviewsCheckBoxList.Items[0].Selected = true; //weird bug fix...
        return;
    }

    string[] sCheckedBox = sResult.Split('$');
    //get the index of the item that was checked/unchecked
     int i = int.Parse(sCheckedBox[sCheckedBox.Length - 1].Split('_')[1]);
     if (i == 0)
     {
         if (FilterReviewsCheckBoxList.Items[i].Selected == true)
         {
             for (int j = 1; j < FilterReviewsCheckBoxList.Items.Count; j++)
                 FilterReviewsCheckBoxList.Items[j].Selected = true;
         }
         else
         {
            for (int j = 1; j < FilterReviewsCheckBoxList.Items.Count; j++)
                FilterReviewsCheckBoxList.Items[j].Selected = false;
         }
     }
     else
     {
        if (FilterReviewsCheckBoxList.Items[i].Selected == false)
        {
            FilterReviewsCheckBoxList.Items[0].Selected = false;
        }
        else
        {
            //if (oFirstTable != null)
            //{
            //    oTable = oFirstTable;
            //    oView = oTable.DefaultView;
            //}
            bool bAllChecked = true;
            for (int j = 1; j < FilterReviewsCheckBoxList.Items.Count; j++)
            {
                 if (FilterReviewsCheckBoxList.Items[j].Selected == false)
                 {
                     bAllChecked = false;
                     break;
                 }
            }
            if (bAllChecked)
                 FilterReviewsCheckBoxList.Items[0].Selected = true;
        }
    }
}

在更改下拉列表时,是否知道为什么 FilterReviewsCheckBoxList_Selected 被调用(使用下拉列表作为发送方参数)?

1 个答案:

答案 0 :(得分:0)

对于可能遇到同样问题的任何人,修复方法是避免将网格视图静态绑定到实体数据源。而是在页面加载事件中动态绑定网格。