2017-02-08 100 views
0

我已經耗盡了我的google-fu,並回顧了幾個標題類似但不適用的SO問題,現在是時候發佈了。ASP GridView排序事件沒有觸發

我有一個ASP GridView,我希望用戶能夠排序。據我所知,它已正確配置,但單擊列標題時網格無響應,並且服務器端事件處理程序從不被調用。我檢查了瀏覽器的JavaScript錯誤,但沒有記錄。我看到POST請求正在發出,並收到200響應。請求和響應似乎主要由ViewState對象組成。

我的GridView。請注意,這是一個UpdatePanel內,但行爲是一樣的,當我刪除的UpdatePanel

<asp:UpdatePanel runat="server" ID="AssetListUpdatePanel" ChildrenAsTriggers="true" UpdateMode="Conditional" > 
    <ContentTemplate> 
     <asp:GridView ID="GridViewList" runat="server" AllowPaging="True" AllowSorting="True" 
      AutoGenerateColumns="False" EnableModelValidation="True" PageSize="50" DataKeyNames="AssetCode" 
      OnRowCommand="GridViewList_RowCommand" BorderColor="#CCCCCC" GridLines="Horizontal" Width="100%" 
      OnPageIndexChanging="GridViewList_PageIndexChanging" OnSorting="GridViewList_Sorting" OnRowDataBound="GridViewListRow_DataBound"> 
      <HeaderStyle HorizontalAlign="Left" /> 
      <PagerSettings Mode="NumericFirstLast" /> 
      <AlternatingRowStyle BackColor="#F4F4F4" /> 
      <Columns> 
       <asp:BoundField DataField="AssetCode" HeaderText="ID" ReadOnly="True" SortExpression="AssetCode" /> 
       <asp:TemplateField HeaderText="Title" SortExpression="Title" ItemStyle-CssClass="wrap"> 
        <ItemTemplate> 
         <%# Eval("Title") %><br /> 
         <asp:HyperLink CssClass="filename" runat="server" ID="FileLink" Target="_blank" /> 
        </ItemTemplate> 
        <ItemStyle CssClass="wrap"></ItemStyle> 
       </asp:TemplateField> 
       <asp:BoundField DataField="Type" HeaderText="Type" ReadOnly="True" SortExpression="Type"> 
        <HeaderStyle HorizontalAlign="Left"></HeaderStyle> 
        <ItemStyle HorizontalAlign="Center"></ItemStyle> 
       </asp:BoundField> 
       <asp:BoundField DataField="DateCreated" HeaderText="Created" ReadOnly="True" SortExpression="DateCreated"> 
        <HeaderStyle HorizontalAlign="Left"></HeaderStyle> 
        <ItemStyle HorizontalAlign="Center"></ItemStyle> 
       </asp:BoundField> 
       <asp:BoundField DataField="DateModified" HeaderText="Modified" ReadOnly="True" SortExpression="DateModified"> 
        <HeaderStyle HorizontalAlign="Left"></HeaderStyle> 
        <ItemStyle HorizontalAlign="Center"></ItemStyle> 
      </Columns> 
      <RowStyle HorizontalAlign="Left" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> 
     </asp:GridView> 
    </ContentTemplate> 
</asp:UpdatePanel> 

我已經做了明顯的....設置AllowSorting爲真,定義的OnSorting事件處理程序,並指定排序表達式爲每列。

而且我選的事件處理程序(再次,永遠不會被調用):

protected void GridViewList_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    ViewState["page"] = null; 

    const string DESC = "DESC"; 
    const string ASC = "ASC"; 


    string SortDirection = DESC; 
    if (ViewState["sort"] != null && 
     ViewState["sort"].ToString() == e.SortExpression && 
     (ViewState["sortDirection"] == null || 
     ViewState["sortDirection"].ToString() != ASC)) 
    { 
     SortDirection = ASC; 
    } 

    ViewState["sort"] = e.SortExpression; 
    ViewState["sortDirection"] = SortDirection; 

    FilterByCriteria(); 

    //now that the grid is sorted, apply a style to the sorted column to 
    //show the sort status and direction. 
    var sortedColumnIdx = GetColumnIndex(GridViewList, e.SortExpression); 
    var styleName = SortDirection.Equals(DESC) ? "sortdesc" : "sortasc"; 
    GridViewList.HeaderRow.Cells[sortedColumnIdx].CssClass = styleName; 
} 

而且在FilterByCriteria網格重新綁定()方法(請不要給我談任何高射炮命名!)

private void FilterByCriteria() 
{ 
    DataTable dataTable; 
    //retrieve data from DB. 
    dataTable = ConvertToDataTable(FilteredAssets()); 
    DataView dataView = new DataView(dataTable); 

    // attach dataview to gridview 
    GridViewList.DataSource = dataView; 

    // bind 
    GridViewList.DataBind(); 
} 

我明顯缺少一些基本的東西在這裏,但我沒有看到它。值得注意的是,這個GridView的分頁行爲是一樣的......也就是說它不是。 GridView沒有響應,並且從不調用PageIndexChanging處理程序。

+1

而不是在標記中配置事件處理程序,你可以嘗試添加它們在Page_Init中,看看是否有什麼區別? – sh1rts

+0

簡單地將排序處理程序移動到Page_Init似乎已解決了這兩個處理程序的問題,這非常奇怪。你有什麼洞察到什麼和因此? – damion

+1

不知道*爲什麼*你的網格事件不是誠實的解僱,而是將代碼中的所有東西連接起來,而不是將事件名稱添加到標記中,這一直是我對webforms的偏好,並一直爲我工作。 – sh1rts

回答

1

你不排序數據,只是更新sortsortDirection ViewStates(除非它發生在FilterByCriteria)。但是GridView可能沒有實際的變化,所以看起來好像排序方法從未被觸發。

//get the data again, either from source or ViewState 
DataTable dt = getData(); 
DataTable dt = ViewState["dt"] as DataTable; 

//do the sorting 
dt.DefaultView.Sort = ViewState["sort"] + " " + ViewState["sortDirection"]; 

//rebind the data to the gridview 
GridView1.DataSource = dt; 
GridView1.DataBind(); 
+0

感謝您的輸入。事實證明,你有1/3的問題。更新重新綁定數據以明確排序數據視圖的方法是修復程序的一部分。出於某種原因,我會明白GridView,而不是DataView對此負責。今天晚些時候我會發布完整的解決方案。 – damion