自定義DataGrid分頁設置C#

 Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="DictList" method="post" runat="server">
      <TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" border="1">
    <TR>
     <td bgColor="#c0c000">信息:<FONT face="Arial" color="#ffffff">數據維護</FONT>
     </td>
    </TR>
    <tr>
     <td><FONT face="宋體"></FONT></td>
    </tr>
    <tr>
     <td><asp:datagrid id="MyDataGrid" runat="server" Width="100%" PageSize="2" AllowPaging="True" AutoGenerateColumns="False" DataKeyField="FDictid">
       <SelectedItemStyle BackColor="#FFC080"></SelectedItemStyle>
       <HeaderStyle BackColor="#C0C000"></HeaderStyle>
       <Columns>
        <asp:ButtonColumn Text="選擇" HeaderText="選擇" CommandName="Select">
         <HeaderStyle Font-Bold="True" HorizontalAlign="Center" Width="8%"></HeaderStyle>
         <ItemStyle Font-Bold="True" HorizontalAlign="Center"></ItemStyle>
        </asp:ButtonColumn>
        <asp:BoundColumn DataField="FDictID" SortExpression="FDictID asc" HeaderText="標識號">
         <HeaderStyle Width="15%"></HeaderStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="FNameCn" SortExpression="FNameCn asc" HeaderText="名稱">
         <HeaderStyle Width="15%"></HeaderStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="FNameEn" SortExpression="FNameEn asc" HeaderText="英文名稱">
         <HeaderStyle Width="15%"></HeaderStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="FNote" SortExpression="FNote asc" HeaderText="描敘">
         <HeaderStyle Width="47%"></HeaderStyle>
        </asp:BoundColumn>
       </Columns>
       <PagerStyle Visible="False"></PagerStyle>
      </asp:datagrid></td>
    </tr>
   </TABLE>
   <TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" bgColor="#ff9966" border="1">
    <TR>
     <td align="right">
      <asp:linkbutton id="btnFirst" runat="server" CommandArgument="fist"
             οnclick="btnFirst_Click">首頁</asp:linkbutton>  
      <asp:linkbutton id="btnPrev" runat="server" CommandArgument="prev"
             οnclick="btnPrev_Click">上一頁</asp:linkbutton>  
      <asp:linkbutton id="btnNext" runat="server" CommandArgument="next"
             οnclick="btnNext_Click">下一頁</asp:linkbutton>  
      <asp:linkbutton id="btnLast" runat="server" CommandArgument="last"
             οnclick="btnLast_Click">末頁</asp:linkbutton>  
      <asp:label id="lblCurrentIndex" runat="server"></asp:label>/<asp:label id="lblPageCount" runat="server"></asp:label>    
      跳轉到<asp:DropDownList ID="DropDownList1" runat="server" Width="50px">
         </asp:DropDownList>
         <asp:TextBox id="txtGoPage" runat="server" Width="30px" CssClass="textbox"></asp:TextBox>
      <asp:Button id="btnGo" runat="server" Text="GO" CssClass="button" Width="29px"
             οnclick="btnGo_Click1"></asp:Button></td>
    </TR>
   </TABLE>
  </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection myConnection;
    private void Page_Load(object sender, System.EventArgs e)
    {
        string strConn = "server=KAOO_TIAN-SZ//SQLEXPRESS;uid=UserName;pwd=Password;database=TESTDB";
        // 在此處放置用戶代碼以初始化頁面
        //myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
        myConnection = new SqlConnection(strConn);
        myConnection.Open();
        if (!IsPostBack)
            BindGrid();
    }

    public void BindGrid()
    {
        string strSql = "SELECT * FROM t_dict";
        SqlDataAdapter myCommand = new SqlDataAdapter(strSql, myConnection);
        DataSet ds = new DataSet();
        myCommand.Fill(ds, "t_dict");
        MyDataGrid.DataSource = ds.Tables["t_dict"].DefaultView;
        MyDataGrid.DataBind();
        ShowStatsPage();
    }

    void ShowStatsPage()
  {
   //顯示頁面信息
   lblCurrentIndex.Text = "[當前:第" + ((int)MyDataGrid.CurrentPageIndex+1) + "頁]";
   lblPageCount.Text = "[共" + MyDataGrid.PageCount + "頁]";
   txtGoPage.Text = ((int)MyDataGrid.CurrentPageIndex + 1) + "";
   //DropDownList1.DataSource = "";
   //DropDownList1.DataBind();
   DropDownList1.Items.Clear();
   for (int i = 0; i < MyDataGrid.PageCount; i++)
   {
       DropDownList1.Items.Add(i + 1 + "");
   }
   DropDownList1.SelectedValue = MyDataGrid.CurrentPageIndex + 1 + "";
  }

    private void MyDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
        //處理按下數字的方法
        MyDataGrid.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }

    protected void btnFirst_Click(object sender, EventArgs e)
    {
        MyDataGrid.CurrentPageIndex = 0;
        ShowStatsPage();
        BindGrid();
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        if (MyDataGrid.CurrentPageIndex > 0)
            MyDataGrid.CurrentPageIndex--;
        ShowStatsPage();
        BindGrid();
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
            MyDataGrid.CurrentPageIndex++;
        ShowStatsPage();
        BindGrid();
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
        ShowStatsPage();
        BindGrid();
    }
    protected void btnGo_Click1(object sender, EventArgs e)
    {
        //頁面直接跳轉的代碼
        //if (txtGoPage.Text.Trim() != "")
        if(DropDownList1.Text.Trim() != "")
        {
            int PageI = Int32.Parse(DropDownList1.Text.Trim()) - 1;
            if (PageI >= 0 && PageI < (MyDataGrid.PageCount))
                MyDataGrid.CurrentPageIndex = PageI;
        }
        ShowStatsPage();
        BindGrid();
    }
    //----------------------翻頁代碼結束
}

TESTDB for SQL

Table Name:t_dict
FDictID Int 自動增長
FNameCn Nchar(10)
FNameEn Nchar(10)
FNote Nchar(100)
發佈了24 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章