我有一个启用了分页和排序的 GridView Web 控件。我已将数据源绑定到 SqlDataSource。当我尝试更改页面索引或排序时,出现“GridView 触发事件 [Sorting/PageIndexChanging] 未处理”错误。 SqlDataSource 不是已经具备处理这些事件的能力了吗?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@language = "C#" Debug="true" src="ServerCode/Default.cs" Inherits="Home.Default" %>
<html>
<head>
<title>APTEIT Data Preview</title>
</head>
<body>
<form id = "mainFrm" runat = "server">
<div id = "headersDiv">
<asp:Table id = "headerTbl" runat = "server">
</asp:Table>
</div>
<div id = "dataDiv">
<asp:GridView id = "dataTbl" runat = "server"
ShowHeader="false"
AllowPaging="true"
GridLines="None"
AlternatingRowStyle="altRow"
PagerStyle="pager"
PageSize="50"
>
</asp:GridView>
</div>
</form>
</body>
</html>
隐藏代码
SqlDataSource sqlData = new SqlDataSource("Hidden for security reasons");
Select(DataSourceSelectArguments.Empty);
dataTbl.DataSource = sqlData;
dataTbl.DataBind();
headerTbl.Rows.Add(new TableRow());
你必须重新绑定
GridView
:
private void BindData()
{
SqlDataSource sqlData = new SqlDataSource("Hidden for security reasons");
Select(DataSourceSelectArguments.Empty);
dataTbl.DataSource = sqlData;
dataTbl.DataBind();
headerTbl.Rows.Add(new TableRow());
}
protected void dataTbl_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
dataTbl.PageIndex = e.NewPageIndex;
BindData();
}
在标记中添加到
GridView
:
OnPageIndexChanging="dataTbl_PageIndexChanging"
应该可以。