Code for displaying data in a data table on the asp.net page

Source: Internet
Author: User

The implementation code is as follows: Copy codeThe Code is as follows: using System;
Using System. Collections;
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. Drawing;
Namespace ShowData4
{
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
GridView1.PageSize = 5;/* Number of records displayed by the GridView control on each page */
If (GridView1.Rows. Count! = 0)/* when only one page is displayed for the number of records */
{
Control table = GridView1.Controls [0];
Int count = table. Controls. Count;
Table. Controls [count-1]. Visible = true;
}
}
Protected void GridView1_RowCreated (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. Pager)/* display the rows of the page navigation control */
{
/* Create a button to display hyperlinks on the webpage */
LinkButton Button_IndexFirst = new LinkButton ();
LinkButton Button_IndexLast = new LinkButton ();
LinkButton Button_IndexNext = new LinkButton ();
LinkButton Button_IndexPrevious = new LinkButton ();
/* Add the hyperlink button to the page navigation line */
E. Row. Controls [0]. Controls. Add (Button_IndexFirst );
E. Row. Controls [0]. Controls. Add (new LiteralControl ("");/* use two spaces to separate paging buttons */
E. Row. Controls [0]. Controls. Add (Button_IndexPrevious );
E. Row. Controls [0]. Controls. Add (new LiteralControl (("")));
E. Row. Controls [0]. Controls. Add (Button_IndexNext );
E. Row. Controls [0]. Controls. Add (new LiteralControl (("")));
E. Row. Controls [0]. Controls. Add (Button_IndexLast );
Button_IndexFirst.Text = "first page ";
Button_IndexFirst.CommandName = "first ";
Button_IndexFirst.Click + = new EventHandler (PageButtonClick );
Button_indexprevius.text = "Previous Page ";
Button_indexprevius.commandname = "previous ";
Button_indexprevius.click + = new EventHandler (PageButtonClick );
Button_IndexNext.Text = "next page ";
Button_IndexNext.CommandName = "next ";
Button_IndexNext.Click + = new EventHandler (PageButtonClick );
Button_IndexLast.Text = "last page ";
Button_IndexLast.CommandName = "last ";
Button_IndexLast.Click + = new EventHandler (PageButtonClick );
If (GridView1.PageIndex = 0)
{
If (GridView1.PageCount> 1)/* The number of records must contain more than one page */
{
Button_IndexFirst.Enabled = false;
Button_indexprevius.enabled = false;
}
Else/* only one page is required for the number of records */
{
Button_IndexFirst.Enabled = false;
Button_indexprevius.enabled = false;
Button_IndexNext.Enabled = false;
Button_IndexLast.Enabled = false;
}
}
Else if (GridView1.PageIndex = GridView1.PageCount-1)
{
Button_IndexNext.Enabled = false;
Button_IndexLast.Enabled = false;
}
Else if (GridView1.PageCount <= 0)
{
Response. Write ("no data in the data table! ");
Button_IndexFirst.Enabled = false;
Button_indexprevius.enabled = false;
Button_IndexNext.Enabled = false;
Button_IndexLast.Enabled = false;
}
}
}
Protected void PageButtonClick (object sender, EventArgs e)
{
LinkButton clickedButton = (LinkButton) sender );
If (clickedButton. CommandName = "first")/* click the "first page" button and the page index is 0 */
{
GridView1.PageIndex = 0;
}
Else if (clickedButton. CommandName = "next")/* click the "next page" button and Add 1 to the page index */
{
If (GridView1.PageIndex <GridView1.PageCount-1)
{
GridView1.PageIndex + = 1;
}
}
Else if (clickedButton. CommandName = "previous")/* click the "previous Page" button. if the page index is greater than or equal to 1, the value is reduced by 1 */
{
If (GridView1.PageIndex> = 1)
{
GridView1.PageIndex-= 1;
}
}
Else if (clickedButton. CommandName = "last")/* click the "last page" button */
{
GridView1.PageIndex = GridView1.PageCount-1;
}
}
}
}

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.