asp.net GridView page: First next 1 2 3 4 Last page _ Practical tips

Source: Internet
Author: User
Tags rowcount

Effect Chart:

Function Introduction: You can use the up and down key to select the line, select and click Modify, TextBox get the data of the code in the GridView. For your help, please remember to click on the "good text to the top" Oh!!! Do not understand, please leave a message. Needless to say, the sticker code is as follows:

 

Core code:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using system.data.sqlclient;//please add the following named space using System.Data;

Using System.Drawing; Public partial class _default:system.web.ui.page {SqlConnection con = new SqlConnection ("Server=server\\xxx;database=x xxx User id=xx;
 pwd=xx; "); private int _i = 0;//The change in the grid setting is useful to protected void Page_Load (object sender, EventArgs e) {if (!).
  Page.IsPostBack) {getbind ();
  } protected void Getbind () {String str = ' SELECT * from IM01 ';
  DataSet ds = new DataSet ();
  SqlDataAdapter da = new SqlDataAdapter (str, con); Da.
  Fill (DS); DataTable dt = ds.
  Tables[0];
  Gvdata.datasource = DT;
 Gvdata.databind (); } protected void Gvdata_pageindexchanging (object sender, Gridviewpageeventargs e) {} protected void Gvdata_rowcreate D (object sender, GridViewRowEventArgs e) {if (E.row.rowtype = = Datacontrolrowtype.pager) {Label Label_index = new Label ();
   LinkButton Button_indexfirst = new LinkButton ();
   LinkButton button_indexlast = new LinkButton ();
   LinkButton button_indexnext = new LinkButton ();

   LinkButton button_indexprevious = new LinkButton ();
   Button_indexfirst.text = "First page";
   Button_indexfirst.commandname = "a";
   Button_indexfirst.forecolor = Color.Blue;

   Button_indexfirst.click + = new EventHandler (Pagebuttonclick);
   Button_indexnext.text = "Next page";
   Button_indexnext.commandname = "Next";

   Button_indexnext.forecolor = Color.Blue;

   Button_indexnext.click + = new EventHandler (Pagebuttonclick);
   Button_indexprevious.text = "Previous page";
   Button_indexprevious.commandname = "Previous";
   Button_indexprevious.forecolor = Color.Blue;

   Button_indexprevious.click + = new EventHandler (Pagebuttonclick);
   Button_indexlast.text = "last Pages";
   Button_indexlast.commandname = "Last";
   Button_indexlast.forecolor = Color.Blue;

   Button_indexlast.click + = new EventHandler (Pagebuttonclick);E.row.controls[0]. Controls[0]. Controls[0]. Controls[0].
   Controls.addat (0, (Button_indexfirst)); E.row.controls[0]. Controls[0]. Controls[0]. Controls[0].

   Controls.addat (1, (button_indexprevious)); int controltmp = E.row.controls[0]. Controls[0]. Controls[0].
   controls.count-1; E.row.controls[0]. Controls[0]. Controls[0]. CONTROLS[CONTROLTMP].
   Controls.Add (Button_indexnext); E.row.controls[0]. Controls[0]. Controls[0]. CONTROLS[CONTROLTMP].
  Controls.Add (Button_indexlast); } protected void Gvdata_rowdatabound (object sender, GridViewRowEventArgs e) {if (E.row.rowtype = = Datacontrolrowty Pe.
  DataRow) {//Set hover mouse pointer shape as "small hand" e.row.attributes["style"] = "Cursor:hand";
  } string strgvname = "Gvdata";
  E.row.attributes.add ("id", Strgvname + _i.tostring ());
  E.row.attributes.add ("OnKeyDown", "SelectRow (event, '" + Strgvname + ");");
  E.row.attributes.add ("OnClick", "Markrow" ("+ _i.tostring () +", ' "+ Strgvname +"); ");
  E.row.attributes.add ("TabIndex", "0");
 _i++; } ProtecTed void Pagebuttonclick (object sender, EventArgs e) {LinkButton Clickedbutton = ((LinkButton) sender);
  if (Clickedbutton.commandname = = "I") {gvdata.pageindex = 0; else if (Clickedbutton.commandname = = "Next") {if (Gvdata.pageindex < gvdata.pagecount-1) {GVDATA.P
   Ageindex + 1; } else if (Clickedbutton.commandname = = "Previous") {if (Gvdata.pageindex >= 1) {gvdata.pageindex-
   = 1;
  } else if (Clickedbutton.commandname = "Last") {gvdata.pageindex = gvdata.pagecount-1;
 } getbind ();
  }//Modify protected void Btnupd_click (object sender, EventArgs e) {int intnum = 0; if (this. Num. Text = "" | | This. Num. Text = "0") {Response.Write ("<script type=\" text/javascript\) >alert (' Please consult and choose a piece of information! ')
   </script> ");
  Return else {intnum = Convert.ToInt16 (this. Num.
   Text)-1; Tbvalue.text = This.gvdata.rows[intnum]. CELLS[1].
  Text.tostring (); }
 }
}
Related Article

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.