Asp.net gridview page: First page next page 1 2 3 4 Previous Page last page, gridview last page

Source: Internet
Author: User

Asp.net gridview page: First page next page 1 2 3 4 Previous Page last page, gridview last page

:

Function Description: You can use the up/down key to select a row and click Modify. textbox obtains the code data in the gridview. If it is helpful to you, please remember to click "good text to top !!! Leave a message. No more nonsense. The post code is as follows:

<Head runat = "server"> <title> GridView partition </title> <script type = "text/javascript"> var currentRowId = 0; var styleName = ""; function SelectRow (ev, strGvName) {var e = window. event | ev; var keyCode =-1; if (e. which = null) keyCode = e. keyCode; // IE else if (e. which> 0) keyCode = e. which; // All others if (keyCode = 40) MarkRow (currentRowId + 1, strGvName); if (keyCode = 38) {MarkRow (currentR OwId-1, strGvName);} document. getElementById ("NUM "). value = currentRowId;} function MarkRow (rowId, strGvName) {var Grid = document. getElementById (strGvName); var rowCount = Grid. rows. length; if (document. getElementById (strGvName + rowId) = null) return; if (rowId = rowCount) {return;} if (document. getElementById (strGvName + currentRowId )! = Null) document. getElementById (strGvName + currentRowId ). style. backgroundColor = styleName; currentRowId = rowId; styleName = document. getElementById (strGvName + rowId ). style. backgroundColor; document. getElementById (strGvName + rowId ). style. backgroundColor = 'red'; var obj = document. getElementById (strGvName); obj. rows [rowId]. cells [0]. focus (); document. getElementById ("NUM "). value = currentRowId ;}</script> <style type = "text/css">. hidden {display: none ;}</style> 

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; // Add the following namespace: using System. data; using System. drawing; public partial class _ Default: System. web. UI. page {SqlConnection con = new SqlConnection ("Server = SERVER \ xxx; Database = xxxx; User ID = xx; Pwd = xx;"); private int _ I = 0; // defines the volume of changes. It is useful to query the consumer Grid settings to pr. Otected 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_RowCreated (object sender, GridView RowEventArgs 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 Response"; Button_IndexFirst.CommandName = "first"; Button_IndexFirst.ForeColor = Color. blue; Button_IndexFirst.Click + = new EventHandler (PageButtonClick); Button_IndexNext.Text = "next success"; Button_IndexNext.CommandName = "next"; Button_IndexNext.ForeColor = Color. blue; Button_IndexNext.Click + = new EventHandler (PageButtonClick); button_indexprevius.text = "previous example"; button_indexprevius.commandname = "previous"; button_indexprevius.forecolor = Color. blue; button_indexprevius.cl Ick + = new EventHandler (PageButtonClick); Button_IndexLast.Text = "last vertex"; 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_IndexPrevio Us); 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 = DataControlRowType. dataR Ow) {// set the floating mouse pointer to "little 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 (objec T sender, EventArgs e) {LinkButton clickedButton = (LinkButton) sender); if (clickedButton. commandName = "first") {gvData. pageIndex = 0;} else if (clickedButton. commandName = "next") {if (gvData. pageIndex <gvData. pageCount-1) {gvData. pageIndex + = 1 ;}} else if (clickedButton. commandName = "previous") {if (gvData. pageIndex> = 1) {gvData. pageIndex-= 1 ;}} else if (clickedButton. comma NdName = "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 ('Check your website and select an item first! ') </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.