我的google-fu已经筋疲力尽,并回顾了几个有类似标题但不适用的SO问题,是时候发布了。
我有一个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设置为true,定义了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处理程序。
答案 0 :(得分:1)
您不是要对数据进行排序,只是更新sort
和sortDirection
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();