Select multiple pages in the gridview

Source: Internet
Author: User

JS front-end:

// Implement multiple selections in the gridview
Function checkallc (ocheckbox ){
VaR gridview1 = Document. getelementbyid ('gvdatalist ');
For (I = 1; I <gvdatalist. Rows. length; I ++ ){
Gridview1.rows [I]. cells [0]. getelementsbytagname ("input") [0]. Checked = ocheckbox. checked;
}
}

 

Background:

1: The pageindexchanging event on the page of the gridview calls to obtain information about multiple primary keys. You also need to determine whether data has been selected before turning the page. The specific method is as follows:

The method is as follows:

# Region primary key for storing pagination data in the gridview
/// <Summary>
/// Store the primary key of the page flip data of the gridview
/// </Summary>
Private void rememberoldvalues ()
{
Dictionary <string, string> List = session ["selectedobject"] As dictionary <string, string>;
If (list = NULL)
{
List = new dictionary <string, string> ();
}
Foreach (gridviewrow row in gvdatalist. Rows)
{
Checkbox cb = (checkbox) Row. findcontrol ("chkselect ");
String user = gvdatalist. datakeys [row. rowindex]. value. tostring ();
If (session ["selectedobject"]! = NULL)
List = (Dictionary <string, string>) session ["selectedobject"]; // very important
If (CB. Checked)
{
If (! List. containskey (User ))
{
List. Add (user, "null ");
}
}
Else
{
If (list. containskey (User ))
{
List. Remove (User );
}
}
If (list! = NULL & list. Count> 0)
Session ["selectedobject"] = List;
}

}
# Endregion

 

Page with the gridview:

# Region Paging
Protected void gvdatalist_pageindexchanging (Object sender, gridviewpageeventargs E)
{
Rememberoldvalues ();
// Obtain the control
Gridview thegrid = sender as gridview;
Int newpageindex = 0;
If (E. newpageindex =-3)
{
// Click go.
Textbox txtnewpageindex = NULL;

// The gridview provides more APIs than the DataGrid. You can use bottompagerrow or toppagerrow to obtain the paging block. Of course, headerrow and footerrow are also added.
Gridviewrow pagerrow = thegrid. bottompagerrow;

If (pagerrow! = NULL)
{
// Obtain the text control.
Txtnewpageindex = pagerrow. findcontrol ("txtnewpageindex") as textbox;
}
If (txtnewpageindex! = NULL)
{
// Obtain the index
Newpageindex = int. parse (txtnewpageindex. Text)-1;
}
}
Else
{
// Click another button
Newpageindex = E. newpageindex;
}
// Prevent new index Overflow
Newpageindex = newpageindex <0? 0: newpageindex;
Newpageindex = newpageindex> = thegrid. pagecount? Thegrid. pagecount-1: newpageindex;

// Get the new value
Thegrid. pageindex = newpageindex;

// Rebind
Binditems (0, nodeid, strsearch );
}
# Endregion

 

2: When you click the submit button, you need to obtain the IDs of multiple primary keys in the gridview.

# Region obtain the primary key ID selected by the single-choice button
/// <Summary>
/// Obtain the primary key ID selected by the single-choice button
/// </Summary>
/// <Returns> </returns>
Private void getpkid ()
{
Dictionary <string, string> List = session ["selectedobject"] As dictionary <string, string>;
If (list = NULL)
{
List = new dictionary <string, string> ();
}
Foreach (gridviewrow row in gvdatalist. Rows)
{
Checkbox cb = (checkbox) Row. findcontrol ("chkselect ");
String user = gvdatalist. datakeys [row. rowindex]. value. tostring ();
If (CB. Checked)
{
If (! List. containskey (User ))
{
List. Add (user, "null ");
}
}
Else
{
If (list. containskey (User ))
{
List. Remove (User );
}
}
}
Viewstate ["selectedobject"] = List;
}
# Endregion

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.