⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custompageselecter.aspx

📁 asp.net经典案例资料
💻 ASPX
字号:
<%@ Page Debug="true" Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
  <head>
    <title>自定义DataGrid的导航控件</title>
    <script language="C#" runat="server" >

    // This data container will be initialize when Page is loaded
    DataTable dataTable;

    public void Page_Load()
    {
      string dataSource = "Data Source=localhost;";
      string security = "user id=sa; password=;";
      string initialCatalog = "initial catalog=pubs;";
      string cnnString = dataSource + security + initialCatalog;
      SqlConnection connection = new SqlConnection(cnnString);

      string strSql = "select au_lname, au_fname, city, state, zip from [authors]";
      SqlCommand command = new SqlCommand(strSql, connection);
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = command;
      dataTable = new DataTable();

      try {
        connection.Open();
        adapter.Fill(dataTable);
      }
      catch(SqlException e) {
        Response.Write(e.ToString());
        return;
      } 
      finally {
        connection.Close();
      }

      if(!IsPostBack)
      {
        grid1.DataSource = dataTable;
        grid1.DataBind();
        lblTotalNumber.Text = grid1.PageCount.ToString();
        txtPage.Text = grid1.CurrentPageIndex.ToString();
      }
    }

    public void NextPrevPage(object sender, EventArgs e)
    {
      if(((LinkButton)sender).ID == "PrevButton")
      {
        if(grid1.CurrentPageIndex != 0)
          grid1.CurrentPageIndex --;
      }
      else
      {
        if(grid1.CurrentPageIndex != grid1.PageCount-1)
          grid1.CurrentPageIndex++;
      }
      grid1.DataSource = dataTable;
      grid1.DataBind();
      txtPage.Text = grid1.CurrentPageIndex.ToString();
    }

    public void JumpToPage(object sender, EventArgs e)
    {
      int number = 0;
      try { 
        number = int.Parse(txtPage.Text); 
      }
      catch { return; }

      if(number < 0 || number > grid1.PageCount-1)
      {
        txtPage.Text = grid1.CurrentPageIndex.ToString();
        return;
      }
      grid1.CurrentPageIndex = number;
      grid1.DataSource = dataTable;
      grid1.DataBind();
    }
    </script>
  </head>

  <body>
  <form id="form1" runat="server">
    <h3>自定义DataGrid的导航控件</h3>
    <asp:DataGrid id="grid1" runat="server" AllowPaging="true" PageSize="5">
        <HeaderStyle BackColor="lightblue" Font-Name="Arial" Font-Bold="true" />
        <ItemStyle BackColor="lightyellow"/>
        <PagerStyle Visible="false" />
    </asp:DataGrid>
    <asp:LinkButton Text="上一页" id="PrevButton" 
      OnClick="NextPrevPage" runat="server" />
    <asp:LinkButton Text="下一页" id="NextButton"
      OnClick="NextPrevPage" runat="server" />
    &nbsp;跳转到 <asp:TextBox id="txtPage" width="20pt" 
      OnTextChanged="JumpToPage" AutoPostBack="true" runat="server"/>页,
    &nbsp;总共<asp:Label id="lblTotalNumber" runat="server"/>页

  </form>
  </body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -