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