The gridview control is bound to a flag field with the following values: 0: not reviewed, 1: Approved, 2: not approved, and the ID is saved in the database, the corresponding name is displayed in the gridview.
To obtain the flag number before editing or deleting it, there are two situations: (use databinder in the rowdatabound event. eval (E. row. dataitem, "flag "). tostring (); obtain)
(1) If the displayed gridview is not converted and the serial number is displayed directly, you must obtain the serial number when editing or deleting it. This is very simple.
Use the gridview. Rows [E. rowindex]. cells [N]. Text in the rowdeleting event
Use the gridview. Rows [E. neweditindex]. cells [N]. Text in the rowediting event
(2) If the gridview is converted during display, it is not displayed as a serial number but as a Chinese character. If you want to get a serial number during editing or deletion, this serial number is not a primary key, you can set commandargument for the edit or delete button.
In HTML: <Asp: templatefield headertext = "edit">
<Edititemtemplate>
<Asp: linkbutton id = "lbtnupdate" runat = "server" commandname = "Update"> Update </ASP: linkbutton>
<Asp: linkbutton id = "lbtncancel" runat = "server" causesvalidation = "false" commandname = "cancel"> cancel </ASP: linkbutton>
</Edititemtemplate>
<Itemtemplate>
<Asp: imagebutton id = "imagebutton1" runat = "server" causesvalidation = "false" commandargument = '<% # databinder. eval (container. dataitem, "flag") %>'
Imageurl = "~ /Images/edit.gif "commandname =" edit "/>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "delete">
<Itemtemplate>
<Asp: imagebutton id = "lbtndelete" runat = "server" causesvalidation = "false" commandname = "delete" commandargument = '<% # databinder. eval (container. dataitem, "flag") %>'
Imageurl = "~ /Images/delete.gif "onclientclick =" Return confirm ('Are you sure you want to delete it? ') "/>
</Itemtemplate>
</ASP: templatefield>
C # medium: Protected void gvreward_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
String strflag = (imagebutton) gvreward. rows [E. rowindex]. findcontrol ("lbtndelete ")). commandargument; // set commandargument in HTML to bind it to the falg field value.
// String strflag = gvreward. rows [E. rowindex]. cells [7]. text. tostring (). trim (); // obtain the value of the 'flag' field bound to the current row.
If (strflag = "1 ")
{
Scriptmanager. registerstartupscript (this. Page, typeof (PAGE), "", "alert ('this Information Department has been approved and cannot be deleted ')", true );
}
Else
{
Int IID = convert. toint32 (gvreward. datakeys [E. rowindex]. value );
Try
{
If (studentreward. removerewardlist (IID ))
{
Scriptmanager. registerstartupscript (this. Page, typeof (PAGE), "", "alert ('deleted successfully')", true );
Bindgridview ();
}
Else
{
Scriptmanager. registerstartupscript (this. Page, typeof (PAGE), "", "alert ('deletion failed')", true );
}
}
Catch (exception ex)
{
Scriptmanager. registerstartupscript (this. Page, typeof (PAGE), "", "alert ('deletion exception occurred ')", true );
}
}
} Protected void gvreward_rowediting (Object sender, gridviewediteventargs E)
{
String strflag = (imagebutton) gvreward. rows [E. neweditindex]. findcontrol ("imagebutton1 ")). commandargument; // set commandargument in HTML to bind it to the falg field value.
If (strflag = "1") // if the review fails, it can be modified. However, the review status must be changed to "0" and not reviewed.
{
Scriptmanager. registerstartupscript (this. Page, typeof (PAGE), "", "alert ('this Information Department has been approved and cannot be modified ')", true );
}
Else
{
Gvreward. editindex = E. neweditindex;
Bindgridview ();
}
}