- Pass a value between two gridview controls (method 1)
1. Set the column in gridview1 as the template column passed as the parameter value:
<Asp: templatefield headertext = "region" sortexpression = "territory">
<Itemtemplate>
<Asp: linkbutton id = "linkbutton1" runat = "server"
Oncommand = "gridviewpoliconclick"
Commandargument = '<% # (string) databinder. eval (container, "dataitem. territory") %>'
Commandname = "territorydrill"
TEXT = '<% # (string) databinder. eval (container, "dataitem. territory") %>'> </ASP: linkbutton>
</Itemtemplate>
<Itemstyle Height = "15px" horizontalalign = "Left" width = "150px"/>
</ASP: templatefield>
2. Obtain the row index number clicked in gridview1:
Protected void gridview1_onclick (Object sender, commandeventargs E)
{
String territoryname = E. commandargument. tostring (). Trim ();
Linkbutton lB = (linkbutton) sender;
Datacontrolfieldcell dcfc = (datacontrolfieldcell) lb. parent;
Gridviewrow gvr = (gridviewrow) dcfc. parent;
Gridview1.selectedindex = gvr. rowindex;
}
3. Set the parameter value of the data source of gridview2:
<Selectparameters>
<Asp: controlparameter controlid = "gridview1" name = "territory"
Propertyname = "selectedvalue" defaultvalue = "" type = "string"/>
</Selectparameters>
In this way, when you click the linkbutton control in a row in gridview1, The gridviewdomainonclick event is triggered, and the index number of the clicked row is obtained. When the value of gridview1.selectedindex changes, the databound event of the gridview2 control is triggered, and the index number of the clicked row is passed to the parameter of the data source of gridview2. in this way, you can click gridview1 to retrieve related data in gridview2.
- Pass a value between two gridview controls (method 2)
The session variable is used to transmit values between two controls.
Set the parameter value of the formview1 Data source:
<Selectparameters>
<Asp: sessionparameter name = "employeeid" sessionfield = "employeeid" type = "int32"/>
</Selectparameters>
Protected void gridview2_onclick (Object sender, commandeventargs E)
{
String [] territoryname = E. commandargument. tostring (). Trim ();
Session ["employeeid"] = territoryname;
Formview1.databind ();
}
When you click the value column in the row in gridview2, take out the parameter of the linkbutton control in the row and save it to session ["employeeid"], and trigger the databound event of formview1. in this way, click gridview2 to retrieve relevant data in formview1.