C # and SQL data browsing paging

Source: Internet
Author: User
pagination | data C # and SQL data browsing paging

If you need to consider such as time filter, other conditions to join, you can edit in the SQL statement, the normal web site, the following data browsing paging

It's OK.

ASPX code:

<%@ Page language= "C #" codebehind= "StockOrderFormBrower.aspx.cs" autoeventwireup= "false" inherits= "GSP. Stockorderformbrower "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>

<HEAD>
<title>
Data browsing paging with C # and SQL
</title>
<link href= "Css/main.css" type= "Text/css" rel= "stylesheet" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<meta content= "Microsoft Visual Studio. NET 7.1" name= "generator" >
<meta content= "C #" Name= "Code_language" >
<meta content= "JavaScript" name= "vs_defaultClientScript" >
</HEAD>

<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<table id= "ShowData" cellspacing= "0" cellpadding= "0" align= "center" border= "0" >
<%showdata ();%><!--output Data-->
</table>
<table align= "Right" >
<tr>
<td>
<%pageload_count ();%>
<input id= "First" type= "button" value= "|<" name= "PRIMARY" runat= "server" ><!--page One-->
<input id= "Prior" type= "button" value= "<" name= "prior" runat= "Server" ><!--previous page-->
<input id= "Next" type= "button" value= ">" name= "Next" runat= "Server" ><!--next page-->
<input id= "Last" type= "button" value= ">| "Name=" runat= "Server" ><!--last page-->
</td>
</tr>
</table>
</form>
</body>

</HTML>

Aspx.cs Code:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;

Namespace ASWBLM
{
 ///<summary>
 /// 
 ///</summary>
  public class UnionInfo:System.Web.UI.Page
 {                                     
  protected System.Web.UI.HtmlControls.HtmlInputButton First
  protected System.Web.UI.HtmlControls.HtmlInputButton Prior;
  protected System.Web.UI.HtmlControls.HtmlInputButton last;
  protected System.Web.UI.HtmlControls.HtmlInputButton Next;


protected static int currentpage = 1;//initialization Start Page
protected static int rowcount = 0/How many articles on this page
private static BOOL Isprior = false;//has "previous page"
private static BOOL Isnext = false;//has "next page"
private static BOOL Islast = false;//has "last page"
protected static int not_shown_records=0;//calculation does not show number of records
private static string Startid = "";//Set previous page start ID
private static string endid = "";//Set next page End ID

private static int page_count = 10;//Initialization page Record count


private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (! IsPostBack)
{
This. Countrecord (). ToString ()//Total Records
This. Page_count (). ToString (),//Total Paging

Init_brower ()//Initialize Browse
}
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.first.ServerClick + = new System.EventHandler (This.first_serverclick);
This.prior.ServerClick + = new System.EventHandler (This.prior_serverclick);
This.next.ServerClick + = new System.EventHandler (This.next_serverclick);
This.last.ServerClick + = new System.EventHandler (This.last_serverclick);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion


<summary>
Display data
</summary>
protected void ShowData ()
{
DataSet ds = new DataSet ();//DataSet
ASWBLM. Include.unioninfo_provider _uip = new ASWBLM. Include.unioninfo_provider ();
String vsql = "";
Vsql = Getsqlcommond (Vsql,startid,endid);

ds = _uip. Showallunioninfo (Vsql)//Data set with all data obtained

Try
{
Response.Write ("<p align=center>");
foreach (DataRow Dr in DS. tables["Table"]. Rows)
{
Response.Write ("<tr align = center onmouseover = \ this.bgcolor = ' #cccccc '" onmouseout = \ "this.bgcolor="; \ ">");

Response.Write ("<td align=\" left\ "width=\" 60%\ "><font color=\" #00309C \ ">");
Response.Write ("<a href=\" unioninfo_read.aspx?id= "+dr[" id "]. ToString () + "\" "Target=\" _self\ ">");
Response.Write (dr["Title"). ToString ());

Response.Write ("</a>");
Response.Write ("</td>");

     response.write ("<td align=\" right\ ">");
     response.write ("<font color=\" #999999 \ ">");
     response.write ("(" +dr["Summarydatetime"). ToString () + ")");
     response.write ("     read" +dr["Readtimes"]. ToString () + "Times");
     response.write ("</font>");
     response.write ("</td>");

     response.write ("</tr>");
    }              &NBSP;&NBSP;&NBSP
    response.write ("</p>");
    startid = ds. tables["Table"]. Rows[0]. Itemarray[0]. ToString ();      //through the array, take the first data, get the start number "Startid"
     RowCount = ds. tables["Table"]. defaultview.count;//gets the number of rows of the table
    endid = ds. tables["Table"]. ROWS[ROWCOUNT-1]. Itemarray[0]. ToString ();//through the array, take the last data, get the end number "Endid"
   }
   catch (SqlException e)
   {     
    response.write (e.message);
&NBSP;&NBSP;&NBSP}
  }


  ///<summary>
  ///Calculate the number of records not displayed
  ///</summary>
   ///<returns></returns>
  protected void Notshownrecords ()
  {
   not_shown_records = this. Countrecord () * * Query Total record number * * (currentpage/* current page/1) * page_count/* per page record number * *;
  }


<summary>
For output information
</summary>
protected void Pageload_count ()
{
This. Notshownrecords ();
Response.Write ("Total" +this. Countrecord () + "record");
Response.Write ("Total" +this). Page_count () + "page");
Response.Write ("First" +currentpage.tostring () + "page");
Response.Write ("A total of" +rowcount.tostring () + "record");
}

  ///<summary>
  ///get Total records total
  ///</summary>
   Total number of records in <returns> time range intcount</returns>
  protected int countrecord ()
   {   
   int intcount = 0;
   sqlconnection Sqlcon = new SqlConnection (COMMON._DBCONNSTR);
   sqlcon.open ();
   
   //Find the total number of records in the condition range
   string strcount = "SELECT count (*) From Unioninfo ";
   
   //Find the first record that meets the criteria
   //string strnum = "SELECT top 1 Id From Unioninfo ";

   sqlcommand Mycomm = new SqlCommand (Strcount,sqlcon);
   sqldatareader dr = Mycomm.executereader ();//Read data stream
   if (Dr. Read ())
   {
                intcount = Int32.Parse (dr[0]. ToString ());
   }
   else
   {
               intcount = 0;
   }
   dr. Close ();
   sqlcon.close ();               
   return intcount;
  .}


<summary>
Total number of pages
</summary>
<returns> Pagination Total </returns>
protected int Page_count ()
{
int pagesum = Total 0;//Paging
Pagesum = this.           Countrecord ()/page_count; Total number of records/pages paged
if (this. Countrecord ()% Page_count) > 0) pagesum++;
return pagesum;
}


<summary>
Get SQL statement
</summary>
<param name= "Vcmd" > Return command line </param>
<returns></returns>
private string Getsqlcommond (string vcommond,string startid,string endid)
{
This. Notshownrecords ()//Do not show rows

Vcommond = "SELECT Top" +page_count+ "{0},{1},{2},{3} from [Unioninfo]";

if (Isprior)//Judge "Previous page"
{

}

if (isnext)//Judge "next page"
{

}

if (islast)//Judge "last page"
{

}

Vcommond = string. Format (Vcommond, "Id", "Title", "Summarydatetime", "readtimes");//This is the field of the datasheet
return vcommond;
}


<summary>
Enter the status of the button to make it available
</summary>
<param name= "First" > The Status of Page one </param>
<param name= "Prior" > previous page Status </param>
<param name= "Next1" > next page Status </param>
<param name= "Last" > final page Status </param>
protected void Setbuttonstate (bool first_,bool prior_,bool next_,bool last_)
{
if (currentpage==1)//to "first page"
{
First. Disabled = true;//First page state
Prior. Disabled = true;//Previous page status
Next.   Disabled = false; Next page status
Last. Disabled = false; Last page status
}
else if (currentpage==this. Page_count ())//To "last page"
{
First. Disabled = false;//First page state
Prior. Disabled = false;//Previous page status
Next.   Disabled = true; Next page status
Last. Disabled = true; Last page status
}
Else
{
First. Disabled = first_;//First page state
Prior. Disabled = prior_;//Previous page status
Next.   Disabled = Next_; Next page status
Last. Disabled = Last_; Last page status
}
}

<summary>
First Page button
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void First_serverclick (object sender, System.EventArgs e)
{
CurrentPage = 1;
This. Setbuttonstate (True,true,false,false);
Startid = "";
Endid = "";
RowCount = ' 0 ';
Islast = false;
Isprior = false;
Isnext = false;
}


  ///<summary>
  ///prev button
  ///</summary>
   ///<param name= "sender" ></PARAM>
  ///<param name= "E" ></PARAM>
  private void Prior_serverclick (object sender, System.EventArgs e)
  {
    if (currentpage = = 1)//Determines whether the current page is 1
   {
    this. Setbuttonstate (True,true,false,false);
   }
   else
   {
    currentpage=currentpage-1;//Current page Self-minus
    this. Setbuttonstate (False,false,false,false);
   }
   isprior = true;
   isnext = false;
   islast = false;  
  }


  ///<summary>
  ///last page
  ///</summary>
   ///<param name= "sender" ></PARAM>
  ///<param name= "E" ></PARAM>
  private void Last_serverclick (object sender, System.EventArgs e)
  {         
   currentpage=this. Page_count ();//to the last page of
   this. Setbuttonstate (false,false,true,true);
   islast = true;
   isprior = false;
   isnext = false;
  }


  ///<summary>
  ///next
  ///</summary>
   <param name= "Sender" ></param>
  ///<param name= "E" ></PARAM>
   private void Next_serverclick (object sender, System.EventArgs e)
  {                       
   if (currentpage = = this. Page_count ())//Determines whether the current page is the total paging
   {
    this. Setbuttonstate (false,false,true,true);
   }
   else
   {
    currentpage=currentpage + 1;//"Current Page" Add
    this to your own. Setbuttonstate (False,false,false,false);
   }
   isnext = true; 
   islast = false;
   isprior = false;
  }


<summary>
Initial browse button
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Init_brower ()
{
CurrentPage = 1;//must be starting from the first page
if (currentpage = 1) && (this. Page_count () = = 1))
{
First. Disabled = true;//First page state
Prior. Disabled = true;//Previous page status
Next. Disabled = true;//Next page state
Last. Disabled = true; Last page status
}
Else
{
First. Disabled = true;//First page state
Prior. Disabled = true;//Previous page status
Next. Disabled = false;//Next page state
Last. Disabled = false; Last page status
}
Startid = "";//Start number
Endid = "";//End number
Islast = false;
Isprior = false;
Isnext = false;
}
}
}

This article does not list SQL statements, is to hope that I and my good friends of the fruits of the labor has been used for commercial purposes, but also hope that you write their own hands, there should be no problem




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.