Several pages in ASP. NET and several pages in ASP. NET

Source: Internet
Author: User

Several pages in ASP. NET and several pages in ASP. NET


1. Implement paging through the DataGrid Control

The DataGrid Control in ASP. NET comes with the paging function. After the data source of the DataGrid is bound, you need to set the DataGrid Control:

Left-click the arrow in the upper-right corner of the control → property generator:

Select the tab on the left

Select allow pagination. Page size indicates how many data entries are displayed on a page. The navigation button's position options include top, bottom, and top/bottom, mode: page number and "Previous Page and next page button". If the page number mode is selected, the value button indicates the maximum number of buttons displayed. If another mode is selected, "Previous Page" and "next page" can be edited into other texts.

The following code adds and binds the data source to the DataGrid Control:

For convenience, first write a function to query data and bind it to the DataGrid BindToGridView ():

Private void BindToDataGrid () {SqlConnection conn = DB. createConnection (); // defines the database connection object conn. open (); // Open the database connection string plain text = "select * from T_Employee"; // query all information from the employee table SqlCommand cmd = new SqlCommand (plain text, conn ); // define the command object SqlDataAdapter sda = new SqlDataAdapter (cmd); // define the adapter object DataTable dt = new DataTable (); // define the dt able table dt sda. fill (dt); // Fill in the dt able table dt DataGrid1.DataSource = dt; // set the data source of DataGrid1 to dt DataGrid1.DataBind (); // bind the data source to the DataGrid}
In this case, the paging effect is displayed:

However, when you click the next page, the page does not jump to the next page. At this time, the PageIndexChanged event of the control is triggered. Therefore, you need to re-bind the control after clicking the button:

Protected void maid (object source, maid e) {this. Maid = e. NewPageIndex; // jump to the specified new page BindToDataGrid (); // rebind control}


2. Implement paging through System. Web. UI. WebControls. PagedDataSource

The PageDataSource class encapsulates paging-related attributes. Through the PageDataSource class, Repeater and DataList can be displayed by PAGE, and Repeater and DataList are also more diverse than DataGrid display.

Define a static variable curPage and pageSize to indicate the current page and page size respectively:

Private static int curPage = 1; // the first page of private static int pageSize = 3 is displayed by default; // three data entries are displayed on each page by default.


For convenience, first write a function DataBindToRepeter () that pagination based on the current page and page size and binds data ():

Private void DataBindToRepeter () {SqlConnection conn = DB. createConnection (); // defines the database connection object conn. open (); // Open the database connection string plain text = "select * from People"; // query all information from the employee table SqlCommand cmd = new SqlCommand (plain text, conn ); // define the command object SqlDataAdapter sda = new SqlDataAdapter (cmd); // define the adapter object DataTable dt = new DataTable (); // define the dt able table dt sda. fill (dt); // Fill in the data table dt int curPage = int. parse (this. label2.Text); System. web. UI. webControls. pagedDataSource PPS = new PagedDataSource (); // instantiate the PageDataSource class pds. dataSource = dt. defaultView; // obtain the dt filter view. allowPaging = true; // allows pagination of the PNS. pageSize = 3; // The page size is 3 pds. currentPageIndex = curPage-1; // current page number, starting from scratch Repeater1.DataSource = pds; // set PageDataSource instance ps to Repeater1.DataBind (); // bind data source}

Here, you can manually add the previous page (btnPre) and next page (btnNext) buttons. Each time you click the button, subtract one or more of the curPage values based on the events and re-bind them:

Protected void btnPre_Click (object sender, EventArgs e) {curPage = curPage-1; // front flip DataBindToRepeter (); // rebind} protected void btnNext_Click (object sender, EventArgs e) {curPage = curPage + 1; // flip back to DataBindToRepeter (); // rebind}

PageDataSource paging principle: PageDataSource is equivalent to extracting data first, and thenPage sizeAndCurrent page to be displayedPage and display. Because we need to extract all the data first, the performance of this method is not very reasonable when the data volume to be displayed is large.


3. Paging through stored procedures

The stored procedure is used to implement paging. Based on the conditions, only the data on the page to be displayed is extracted from the database, which involves a total of 100 data records in the database, how to extract 50th to 60th pieces of data from the database. At this time, I thought about the true and false pagination in the video and how to extract several consecutive data entries in the table.

The program defines the following variables pageSize (How many records are displayed on each page) and curPage (the current page ). By doing so, we can calculate the number of data records in the database from beginPos to endPos ).

The relationship between these variables is beginPos = (curPage-1) × pageSize + 1

EndPos = curPage × pageSIze

The core code of the stored procedure is:

Create procedure PRO_Paging -- display @ beginPos int starting from the data entry, -- display to the end of the data entry @ endPos intASBEGINSET nocount on; -- Here we use a temptb table with temptb as (select ROW_NUMBER () over (order by pID) as rowNumber, * from People) that is used to sort each row of data) -- query the data from entry @ beginPos to entry @ endPos select * from temptb where rowNumber between @ beginPos and @ endPos ENDGO

The advantage of this method is that after specifying the number of pages to be displayed and the size of each page, you only need to extract the number of pages to be displayed from the database, you do not need to extract a large amount of data, that is, the "real paging" mentioned in the niuyun video. The previous method is to retrieve all the data by using the "false paging" method ".




In ASPNET, how can I use C # code to implement paging?

The
Put a DataList control on the foreground
<Asp: DataList ID = "DataList1" runat = "server">
<ItemTemplate>
Repeat the content here
</ItemTemplate>
</Asp: DataList>
<Div id = "pages"> current page: <asp: Label ID = "labPage" runat = "server" Text = "1"> </asp: label>/<asp: Label ID = "labBackPage" runat = "server"> </asp: Label>
<A id = "btnOne" runat = "server"> </a>
<A id = "btnUp" runat = "server"> </a>
<A id = "btnNext" runat = "server"> </a>
<A id = "btnBack" runat = "server"> </a>
</Div>
In the. cs background...
Protected void bind ()
{
Int ...... remaining full text>

Pages In ASPNET

Private void MyBind ()
{
SqlConnection con = new SqlConnection (ConfigurationManager. ConnectionStrings ["Yuming"]. ConnectionString );
DataTable dt = new DataTable ();
System. Text. StringBuilder sb = new System. Text. StringBuilder ();
If (Session ["idi"]. ToString () = "1 ")
{
Sb. Append ("select * from manager where 1 = 1 ");
}
Else
{
Sb. Append ("select * from manager where yewuid = '" + Session ["id"] + "'");
}
If (txtYewu. Text! = "")
{
Sb. Append ("and yewu like '%" + txtYewu. Text + "% '");
}
If (txtym. Text! = "")
{
Sb. Append ("and yname like '%" + txtym. Text + "% '");
}
If (txtzcdw. Text! = "")
{
Sb. Append ("and ycompany like '%" + txtzcdw. Text + "% '");
}
If (txtzcs. Text! = "")
{
Sb. Append ("and yuser like '%" + txtzcs. Text + "% '");
}
If (txtk. Text! = "")
{
Sb. Append ("and ytime = '" + txtk. Text + "'");
}
If (txto. Text! = "")
{
Sb. Append ("and otime = '" + txto. Text + "'");
}
SqlDataAdapter da = new SqlDataAd... the remaining full text>

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.