我已經耗盡了我的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處理程序。
而不是在標記中配置事件處理程序,你可以嘗試添加它們在Page_Init中,看看是否有什麼區別? – sh1rts
簡單地將排序處理程序移動到Page_Init似乎已解決了這兩個處理程序的問題,這非常奇怪。你有什麼洞察到什麼和因此? – damion
不知道*爲什麼*你的網格事件不是誠實的解僱,而是將代碼中的所有東西連接起來,而不是將事件名稱添加到標記中,這一直是我對webforms的偏好,並一直爲我工作。 – sh1rts