lhsunrice

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            LoadProductData(GridView1);
            LoadProductData(GridView2);
        }
    }  
   
    void LoadProductData(GridView grid)
    {
        DataTable dt = CreateSampleData();

        grid.DataSource = dt;
        grid.DataBind();
    }

    #region sample data

    static DataTable CreateSampleData()
    {
        DataTable tbl = new DataTable("Items");

        tbl.Columns.Add("ItemID", typeof(int));
        tbl.Columns.Add("ItemName", typeof(string));

        int count = 0;
        while (count++ < 100) {
            tbl.Rows.Add(count, "Item#" + count.ToString("d3"));
        }
       
        return tbl;
    }

    #endregion   
     
   
    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView theGrid = sender as GridView;  // refer to the GridView
        int newPageIndex = 0;

        if (0 > e.NewPageIndex) { // when click the "GO" Button
            TextBox txtNewPageIndex = null;
            TextBox txtNewPageSize = null;
            GridViewRow pagerRow = theGrid.BottomPagerRow; // refer to PagerGroup

            //
            if (null != pagerRow) {
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                txtNewPageSize = pagerRow.FindControl("txtNewPageSize") as TextBox;
            }

            //
            if (null != txtNewPageIndex) {
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
            }
            //
            if (null != txtNewPageSize) {
                int newPageSize = int.Parse(txtNewPageSize.Text);
                theGrid.PageSize = 0 > newPageSize ? 10 : newPageSize;
            }

            // check to prevent form the NewPageIndex out of the range
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
        }
        else {  // when click the first, last, previous and next Button
            newPageIndex = e.NewPageIndex;
        }      

        // specify the NewPageIndex
        theGrid.PageIndex = newPageIndex;
       
        // rebind the control
        LoadProductData(theGrid);
    }

    protected void drpNewPageIndex_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drp = sender as DropDownList;
        GridView grd = drp.NamingContainer.NamingContainer as GridView;
        GridViewPageEventArgs pageArgs = new GridViewPageEventArgs(int.Parse(drp.SelectedValue));
        GridView_PageIndexChanging(grd, pageArgs);
    }

    protected void GridView_DataBound(object sender, EventArgs e)
    {
        GridView grd = sender as GridView;
        GridViewRow pagerRow = grd.BottomPagerRow;
        DropDownList drp = pagerRow.FindControl("drpNewPageIndex") as DropDownList;       
        for (int i = 1; i <= grd.PageCount; i++) {
            drp.Items.Add(new ListItem(i + "/" + grd.PageCount, (i - 1).ToString()));
        }
        drp.SelectedIndex = grd.PageIndex;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET DEMO16: GridViewPagerTemplate</title>
</head>
<body>
    <form id="form1" runat="server">
<div>       
        <h3>Customing GridView\'s Pager Template</h3>
        <input type="button" value="Reload" onclick="location.reload()" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" AllowPaging="true" OnPageIndexChanging="GridView_PageIndexChanging" >
            <PagerTemplate>
                第<%# (((GridView)Container.Parent.Parent).PageIndex + 1) %>/<%# ((GridView)Container.Parent.Parent).PageCount %>页
                每页<asp:textbox id="txtNewPageSize" runat="server" width="20px" text=\'<%# ((GridView)Container.Parent.Parent).PageSize %>\' />项
                共<%# ((DataTable)((GridView)Container.Parent.Parent).DataSource).Rows.Count %>项
                <asp:linkbutton id="btnFirst"  runat="server" Enabled=\'<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>\' causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
                <asp:linkbutton id="btnPrev"  runat="server" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
                <asp:linkbutton id="btnNext"  runat="server" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %>" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />                         
                <asp:linkbutton id="btnLast"  runat="server" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %>" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />                                           
                <asp:textbox id="txtNewPageIndex"  runat="server" width="20px" text=\'<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>\' />
                <asp:linkbutton id="btnGo"  runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" />
            </PagerTemplate>          
        </asp:GridView>       
        <br />
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true" AllowPaging="true" OnPageIndexChanging="GridView_PageIndexChanging" OnDataBound="GridView_DataBound" >
            <PagerTemplate>
                每页<%# ((GridView)Container.Parent.Parent).PageSize %>项
                共<%# ((DataTable)((GridView)Container.Parent.Parent).DataSource).Rows.Count %>项
                <asp:linkbutton id="btnFirst"  runat="server" Enabled=\'<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>\' causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
                <asp:linkbutton id="btnPrev"  runat="server" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex != 0 %>" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
                <asp:linkbutton id="btnNext"  runat="server" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %>" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />                         
                <asp:linkbutton id="btnLast"  runat="server" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %>" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />
                第<asp:DropDownList ID="drpNewPageIndex" runat="server" AutoPostBack="true" CausesValidation="false" OnSelectedIndexChanged="drpNewPageIndex_SelectedIndexChanged">
                </asp:DropDownList>页
            </PagerTemplate>          
        </asp:GridView>
        </div>
  </form>
</body>
</html>

分类:

技术点:

相关文章: