0

基本的に、グリッドビューのフィルタリングオプションを実装したいのですが、列名を含むドロップダウンリストと、その列で使用可能なフィルタリング値を含むチェックボックスリストがあります。別の列を選択するたびに、その列のフィルタリング値をチェックボックスリストにロードする必要があります。私が抱えている問題は、ドロップダウンリストの列を変更すると、チェックリストのイベントが発生し、アプリケーションがクラッシュすることです。

私のコントロールは次のように定義されています。

<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が(送信者引数としてドロップダウンリストを使用して)呼び出される理由について何か考えはありますか?

4

1 に答える 1

0

同じ問題が発生する可能性がある場合の修正は、グリッドビューがエンティティデータソースに静的にバインドされないようにすることです。代わりに、ページ読み込みイベントでグリッドを動的にバインドします。

于 2012-06-28T06:29:38.097 に答える