GridView sorting and paging

by 好大一小白 2010.5.16 18:53
如果在GridView控件上设置 AllowPaging="true" or AllowSorting="true" 而没有使用使用数据源控件 DataSource (i.e. SqlDataSource, ObjectDataSource),运行则会出现下列错误
当在GridView控件上单击下一页时
The GridView GridViewID fired event PageIndexChanging which wasnt handled.
当点排序回出
The GridView GridViewID fired event Sorting which wasnt handled.
如果没有GridViewDataSourceID 的属性,必添加一个操作才可以排序及分。。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">    
    private void PopulatePublishersGridView()
    {
        //gridview binding to datatable  
    }

        //database connection

    private string GridViewSortDirection
    {
        get { return ViewState["SortDirection"] as string ?? "ASC"; }
        set { ViewState["SortDirection"] = value; }
    }

    private string GridViewSortExpression
    {
        get { return ViewState["SortExpression"] as string ?? string.Empty; }
        set { ViewState["SortExpression"] = value; }
    }

    private string GetSortDirection()
    {
        switch (GridViewSortDirection)
        {
            case "ASC":
                GridViewSortDirection = "DESC";
                break;

            case "DESC":
                GridViewSortDirection = "ASC";
                break;
        }

        return GridViewSortDirection;
    }

    protected void gridViewPublishers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridViewPublishers.DataSource = SortDataTable(gridViewPublishers.DataSource as DataTable, true);
        gridViewPublishers.PageIndex = e.NewPageIndex;
        gridViewPublishers.DataBind();
    }

    protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
    {
        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            if (GridViewSortExpression != string.Empty)
            {
                if (isPageIndexChanging)
                {
                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
                }
                else
                {
                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
                }
            }
            return dataView;
        }
        else
        {
            return new DataView();
        }
    }

    protected void gridViewPublishers_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridViewSortExpression = e.SortExpression;
        int pageIndex = gridViewPublishers.PageIndex;
        gridViewPublishers.DataSource = SortDataTable(gridViewPublishers.DataSource as DataTable, false);
        gridViewPublishers.DataBind();
        gridViewPublishers.PageIndex = pageIndex;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        PopulatePublishersGridView();
    }

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView Sorting/Paging without a DataSourceControl DataSource</title>
</head>
<body>
    <form id="form" runat="server">
        <div>
            <asp:GridView ID="gridViewPublishers" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false"
                EmptyDataText="No records found" PagerSettings-Mode="NumericFirstLast" PageSize="25"
                OnPageIndexChanging="gridViewPublishers_PageIndexChanging" OnSorting="gridViewPublishers_Sorting"
                runat="server">
                <AlternatingRowStyle BackColor="LightGray" />
                <HeaderStyle BackColor="Gray" Font-Bold="true" Font-Names="Verdana" Font-Size="Small" />
                <PagerStyle BackColor="DarkGray" Font-Names="Verdana" Font-Size="Small" />
                <RowStyle Font-Names="Verdana" Font-Size="Small" />
                <Columns>
                    <asp:BoundField DataField="PubID" HeaderText="Publisher ID" SortExpression="PubID" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Company Name" HeaderText="Company Name" SortExpression="Company Name" />
                    <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                    <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
                    <asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" />
                    <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
                    <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Tags: , , ,

WEB开发 | ASP.NET

不允许评论

微信赞

本站统计

36 篇文章
5 个单页
12 条评论
12 次评分
1321010 次访问
访问统计开始于 2010年4月24日
平均日访问 224 次
当前 9 人在线

声明

本博所有网友评论不代表本博立场,版权归其作者所有。 
苏ICP备09004001号
Powered by BlogYi.net  edit by 1wanweb.com
© Copyright 2008-2017