GridView分页和排序

时间:2012-02-24 00:19:03

标签: asp.net sorting gridview paging sqldatasource

我有一个GridView webcontrol,它启用了分页和排序。我已将数据源绑定到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());

1 个答案:

答案 0 :(得分:3)

你必须重新绑定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"

它应该有用。