The Edit and delete buttons in the gridview obtain the values of the non-primary key fields of the current row.

Source: Internet
Author: User
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 ();
}
}

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.