Asp.net paging Method

Source: Internet
Author: User

ASP. NET paging is a common problem we encounter during programming. Let's talk about this question.

Q enables the built-in paging function of some data controls, such as setting the "AllowPaging" attribute of the GridView control to "true", and similar data controls include DetailsView.

Q uses an SQL query statement to extract data from a specified part of the page. It also includes calling the built-in stored procedures in the database to extract paging data.

Q: When calling the Fill method of the corresponding data adapter object DataAdapter object), pass the paging parameter to complete the paging function of the dataset.

Q: access the "Rows" attribute of the DataTable object and access the data Rows in the specified segment in the result set in a circular output mode to achieve paging effect.

Analysis]

The paging function is widely used in most WEB projects. This topic examines the knowledge of the available paging methods in ASP. NET. When there are few requirements on page layout, such as the WEB Background management system, using the built-in paging function of Data Control is usually the first choice for programmers. To demonstrate the four commonly used paging methods, create an instance and use these four methods to display the same result set by page on the same page.

Add a new WEB form to the NetWeb3 project in VS 2008 and name it Pager. aspx. add a GridView control to the page to display the built-in paging function. Add two Repeater controls to display the SQL statement pages and data adapter object pages.

Finally, import the System. Data namespace on the Pager. aspx page, and use the for loop to output the specified number of records of the dt Data table to display the results on pages. Write Pager. aspx in code 12.12.

Code 12.12 multiple methods page display page: Pager. aspx

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> example of four paging methods for www.sytm.net website </title>

</Head>

<Body>

<Form id = "form1" runat = "server">

<Div>

<H3> Control built-in paging three records per page]

<Asp: GridView runat = "server" ID = "GView" AllowPaging = "true" PageSize = "3"> </asp: GridView>

<H3> the data adapter page displays 1st pages, with 2 records per page.

<Asp: Repeater runat = "server" ID = "Rp1">

<HeaderTemplate> <ul> </HeaderTemplate>

<ItemTemplate>

<Li> <asp: Label runat = "server" ID = "UName" Text = '<% # Eval "UserName") %>'> </asp: Label>: <asp: Label runat = "server" ID = "Msg" Text = '<% # Eval "Message") %>'> </asp: Label> </li>

</ItemTemplate>

<FooterTemplate> </ul> </FooterTemplate>

</Asp: Repeater>

<H3> SQL page displays 2nd pages, 3 records per page]

<Asp: Repeater runat = "server" ID = "Rp2">

<HeaderTemplate> <ul> </HeaderTemplate>

<ItemTemplate>

<Li> <asp: Label runat = "server" ID = "UName" Text = '<% # Eval "UserName") %>'> </asp: Label>: <asp: Label runat = "server" ID = "Msg" Text = '<% # Eval "Message") %>'> </asp: Label> </li>

</ItemTemplate>

<FooterTemplate> </ul> </FooterTemplate>

</Asp: Repeater>

<H3> the data table is displayed on page 1st, with three records on each page.

<%

Response. Write "<ul> ");

Int I;

String UName;

String Msg;

Fori = 0; I <3; I ++)

{UName = dt. Rows [I] [0]. ToString );

Msg = dt. Rows [I] [1]. ToString );

Response. WriteString. Format "<li> {0 }:{ 1} </li>", UName, Msg ));}

Response. Write "</ul> ");

%>

</Div>

</Form>

</Body>

</Html>

Compile Pager. aspx. cs, as shown in code 12.13.

Code 12.13 multiple methods paging logic code: Pager. aspx. csusing System ;......

// Import necessary namespaces using SQL SERVER Data provider using System. Data. SqlClient; namespace NetWeb3

{Public partial class Pager: System. web. UI. page {// from Web. obtain the database connection string from the 1st sub-nodes of the AppSettings node in config // assign the reference value of the connection string object to the static string variable CnStrstatic string CnStr = ConfigurationManager. appSettings [0]; // create a SqlConnection object based on the CnStr variable, reference as cnSqlConnection cn = new SqlConnectionCnStr); // declare the SqlDataAdapter type variable daSqlDataAdapter da;

// Create a DataSet object and reference it as ds

DataSet ds = new DataSet );

// Declare the variable dt of the DataTable type

// The protected modifier can be ensured in *. in aspx, you can access dtprotected DataTable dt; protected void Page_Loadobject sender, EventArgs e) {this. GView. pageIndexChanging + = new GridViewPageEventHandlerGView_PageIndexChanging); // determines whether the page is loaded for the first time if! IsPostBack)

{// Call the custom MyBind Method

MyBind );

}

}

Protected void MyBind)

{String SqlStr = "SELECT [UserName], [Message] FROM [LeaveMsg]"; // create a new SqlDataAdapter object based on SqlStr and cn and reference it as the da variable da = new SqlDataAdapterSqlStr, cn); // call the da Fill method to Fill in the records to ds, and its DataTable name is LeaveMsg1da. fillds, "LeaveMsg1"); // set the LeaveMsg1 data table to the data source of the GView control.

This. GView. dataSource = ds. tables ["LeaveMsg1"]; // call the da Fill method to populate the records with ds, its DataTable name is LeaveMsg2 // The 2nd parameters received by the Fill method indicate the number of records to be filled, and the 3rd parameters indicate the number of records to be filled. fillds, "LeaveMsg2"); // set the LeaveMsg1 data table to the data source of the Rp1 control.

This. rp1.DataSource = ds. tables ["LeaveMsg2"]. defaultView; cn. open); // write an SQL statement to retrieve data by PAGE during database query. SqlStr = "Select Top 3 [UserName], [Message] from [LeaveMsg] where [id] Not IN Select Top 3*1) [id] from [LeaveMsg]) "; // create a new SqlCommand object based on SqlStr and cn and reference it as the cmd variable SqlCommand cmd = new SqlCommandSqlStr, cn); // call the ExecuteReader method of cmd and return the data reader, reference to drSqlDataReader dr = cmd. executeReaderCommandBehavior. closeConnection); // set dr to the data source of the Rp2 control. this. rp2.DataSource = dr;

// Assign the LeaveMsg1 data table object reference to dt for accessing dt = ds. Tables ["LeaveMsg1"] in *. aspx; // bind the data of all controls on the page

This. DataBind );

// Close the data reader and close the database connection

Dr. Close );

}

// This method is the processing method of the data page index change event of the GView control. protected void GView_PageIndexChangingobject sender, GridViewPageEventArgs e) {// set the new page index GView. PageIndex = e. NewPageIndex;

// Call the MyBind method again to bind page data

MyBind );

}

}

}


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.