PageTemplate——页模板是GridView控件的模板属性之一。PageTemplate用来获取或设置 GridView 控件中页导航行的自定义内容。,在GridView.控件上所显示的空数据行的内容,这个内容可以根据需要来设置。

在对GridView控件启用分页功能时,在GridView 控件下边显示一个页导航行。该页导航行包含允许导航到该控件的不同页面的控件。可以不使用内置页导航行界面,而使用 PagerTemplate 属性定义您自己的用户界面。



在下面的示例中将会在GridView1控件中使用PageTemplate,来自定义GridView1控件页导航信息

 

<script runat="server">

  void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)

  {

    //获取页码在GridView1中的位置

    GridViewRow pagerRow = GridView1.BottomPagerRow;

    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");

    //GridView1的页码更新为PageDropDownList在中选中的页码

    GridView1.PageIndex = pageList.SelectedIndex;

  }

  void GridView1_DataBound(Object sender, EventArgs e)

  {

    //获取页码在GridView1中的位置.

    GridViewRow pagerRow = GridView1.BottomPagerRow;

    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");

    Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

    if(pageList != null)

    {

      for(int i=0; i<GridView1.PageCount; i++)

      {

        //创建一个记录页码的变量

        int pageNumber = i + 1;

        ListItem item = new ListItem(pageNumber.ToString());

        // GridView1控件中的页码信息根着DropDownList下拉框的指的改变而改变

        if(i==GridView1.PageIndex)

        {

          item.Selected = true;

        }

        // GridView1中的页码信息加载到DropDownList

        pageList.Items.Add(item);

      }

    }

    if(pageLabel != null)

    {

      int currentPage = GridView1.PageIndex + 1;    

      pageLabel.Text = " " + currentPage.ToString() +

        "页  共" + GridView1.PageCount.ToString();

    }   

  }

</script>

 

<html  >

  <head id="Head1" runat="server">

    <title>GridView PagerTemplate Example</title>

</head>

<body style="text-align: center">

    <form id="form1" runat="server">

      <h3> PagerTemplate 例子</h3>

      <asp:gridview id="GridView1"

        datasourceid="SqlDataSource1"

        allowpaging="True"

        ondatabound="GridView1_DataBound" 

        runat="server" PageSize="5" Width="447px">

        <pagerstyle forecolor="Blue"

          backcolor="LightBlue"/>

        <pagertemplate>

          <table width="100%">                   

            <tr>                       

              <td style="width:70%">

                <asp:label id="MessageLabel"

                  forecolor="Blue"

                  text="选择页码:"

                  runat="server"/>

                <asp:dropdownlist id="PageDropDownList"

                  autopostback="true"

                  onselectedindexchanged="PageDropDownList_SelectedIndexChanged"

                  runat="server"/>

              </td>  

              <td style="width:70%; text-align:right">

                <asp:label id="CurrentPageLabel"

                  forecolor="Blue"

                  runat="server"/>

              </td>

            </tr>                   

          </table>

        </pagertemplate>

      </asp:gridview>

      <asp:sqldatasource id="SqlDataSource1" 

        selectcommand="SELECT BianH AS 编号, username as 用户名, MingC AS 图片名称 FROM T_XiangC "

        connectionstring="<%$ ConnectionStrings:AjaxDBConnectionString %>"

        runat="server">

      </asp:sqldatasource>

    </form>

  </body>

</html>