国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C# GridView 排序及分頁(yè)

2019-11-18 16:51:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

如果你在GridView控件上設(shè)置 AllowPaging="true" or AllowSorting="true" 而沒(méi)有使用使用數(shù)據(jù)源控件 DataSource (i.e. SqlDataSource, ObjectDataSource),運(yùn)行則會(huì)出現(xiàn)下列錯(cuò)誤:

當(dāng)你在GridView控件上單擊下一頁(yè)時(shí):

The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.

當(dāng)你點(diǎn)擊排序時(shí),則回出現(xiàn):

The GridView 'GridViewID' fired event Sorting which wasn't handled.

如果你沒(méi)有設(shè)置GridView的DataSourceID 的屬性,你必須添加一個(gè)操作才可以排序及分頁(yè)。。


  點(diǎn)這里查看下面的例子

 

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.OleDb" %>

 

<script runat="server">   

    PRivate void PopulatePublishersGridView()

    {

        string connectionString = accessConnectionString();

        OleDbConnection accessConnection = new OleDbConnection(connectionString);

 

        string sqlQuery = "SELECT [PubID], [Name], [Company Name], [Address], [City], [State], [Zip], [Telephone], [Fax], [Comments] FROM Publishers ORDER BY [Name] ASC;";

 

        OleDbCommand accessCommand = new OleDbCommand(sqlQuery, accessConnection);

 

        OleDbDataAdapter publishersDataAdapter = new OleDbDataAdapter(accessCommand);

        DataTable publishersDataTable = new DataTable("Publishers");

        publishersDataAdapter.Fill(publishersDataTable);

 

        int dataTableRowCount = publishersDataTable.Rows.Count;

 

        if (dataTableRowCount > 0)

        {

            gridViewPublishers.DataSource = publishersDataTable;

            gridViewPublishers.DataBind();

        }

    }

 

    private string AccessConnectionString()

    {

        string accessDatabasePath = Server.MapPath("~/App_Data/biblio.mdb");

        return String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", accessDatabasePath);

    }

 

    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" "

<html xmlns="

<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>
http://www.survivalescaperooms.com/cygoodyu/archive/2006/11/29/575825.html


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 富平县| 富顺县| 阿克| 土默特左旗| 东阳市| 新干县| 和硕县| 南江县| 安阳县| 景东| 柏乡县| 濉溪县| 乐山市| 永平县| 庆城县| 宜川县| 长宁县| 沐川县| 侯马市| 甘泉县| 松江区| 武山县| 天祝| 孟连| 霍州市| 论坛| 二连浩特市| 高碑店市| 高密市| 蓬莱市| 垦利县| 望都县| 确山县| 南召县| 泰宁县| 沙湾县| 安吉县| 南涧| 南涧| 长顺县| 南岸区|