.net分頁源碼樣本

來源:互聯網
上載者:User
分頁|樣本 <% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<Script Language="C#" Runat="Server">
SqlConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
 //設定PageSize
 PageSize = 5;
 
 //串連語句
 string MyConnString = "server=127.0.0.1;database=example;uid=sa;pwd=1234567";
 MyConn = new SqlConnection(MyConnString);
 MyConn.Open();

 //第一次請求執行
 if(!Page.IsPostBack)
 {
  ListBind();
  CurrentPage = 0;
  ViewState["PageIndex"] = 0;

  //計算總共有多少記錄
  RecordCount = CalculateRecord();
  lblRecordCount.Text = RecordCount.ToString();

  //計算總共有多少頁
  PageCount = RecordCount/PageSize;
  lblPageCount.Text = PageCount.ToString();
  ViewState["PageCount"] = PageCount;
 }
}
//計算總共有多少條記錄
public int CalculateRecord()
{
 int intCount;
 string strCount = "select count(*) as co from TABLE1";
 SqlCommand MyComm = new SqlCommand(strCount,MyConn);
 SqlDataReader dr = MyComm.ExecuteReader();
 if(dr.Read())
 {
  //intCount = Int32.Parse(dr["co"].ToString());
  intCount=(int)dr["co"];
 }
 else
 {
  intCount = 0;
 }
 dr.Close();
 return intCount;
}

ICollection CreateSource()
{
 
 int StartIndex;
 
 //設定匯入的起終地址
 StartIndex = CurrentPage*PageSize;
 string strSel = "select * from TABLE1";
 DataSet ds = new DataSet();

 SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
 MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
 
 return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
 score.DataSource = CreateSource();
 score.DataBind();
 
 lbnNextPage.Enabled = true;
 lbnPrevPage.Enabled = true;
 if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
 if(CurrentPage==0) lbnPrevPage.Enabled = false;
 lblCurrentPage.Text = (CurrentPage+1).ToString();

}

public void Page_OnClick(Object sender,CommandEventArgs e)
{
 CurrentPage = (int)ViewState["PageIndex"];
 PageCount = (int)ViewState["PageCount"];

 string cmd = e.CommandName;
 //判斷cmd,以判定翻頁方向
 switch(cmd)
 {
  case "next":
   if(CurrentPage<(PageCount-1)) CurrentPage++;
   break;
  case "prev":
   if(CurrentPage>0) CurrentPage--;
   break;
 }

 ViewState["PageIndex"] = CurrentPage;

 ListBind();
 
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />條記錄  
當前為<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />頁 

<asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
 <ItemTemplate>
  姓名:<%# DataBinder.Eval(Container.DataItem,"title") %>
  <asp:LinkButton id="btnSelect" Text="編輯" CommandName="edit" runat="server" />
 </ItemTemplate>
</asp:DataList>
共<%=PageCount%>頁
<asp:LinkButton id="lbnPrevPage" Text="上一頁" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一頁" CommandName="next" OnCommand="Page_OnClick" runat="server" />

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



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。