C # gridview edit & Delete. A confirmation box is displayed when you click Delete.

Source: Internet
Author: User

1. Use the built-in attributes showeditbutton and showdeletebutton of the gridview to set them to true.

<Columns>

...

<Asp: commandfield showeditbutton = "true" showdeletebutton = "true"> </ASP: commandfield>

</Columns>

The linkbutton of edit and delete is displayed at the position of the specified column in The gridview.

2. Add the edit and delete response functions to the gridview.

After you click the edit button, the row record enters the editing mode, and the edit button changes to the update and cancle buttons. Therefore, you need to add three functions related to edit.

Click Delete to bring up a confirmation box. This event needs to be bound in the rowdatabound method. Therefore, you need to add the rowdatabound function for the gridview.

<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" datakeynames = "ID"

...

Onrowdatabound = "gridview1_rowdatabound"
Onrowediting = "gridviewappsrowediting"
Onrowcancelingedit = "gridview1_rowcancelingedit"
Onrowupdating = "gridview1_rowupdating"
Onrowdeleting = "gridview1_rowdeleting"
...

>

3. In rowdatabound, bind the confirmation box for the delete button.

Click Edit Delete before the delete button.

Click Delete to change to update cancle.

When binding a confirmation box event to the delete button, you must first obtain the delete button. In fact, you cannot directly obtain the delete button, you can only use the location of the delete button (for example, the button in the column number of the current row), and then bind the event to this button.

After the confirmation box event is bound to the delete button, the displayed dialog box appears in the row of the gridview: Click Edit-> cancle to display "are you sure you want to delete ?" Confirmation box

In the gridview, rowtype has the header, footer, datarow, and pager types. Generally, only data rows (datarow type) have the delete button. Therefore, you must first determine whether the row is a data row.

Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)

{

If (E. Row. rowtype = datacontrolrowtype. datarow)

{

// For each row of the Data row, the first control in the 6th column is the delete button, and the first control is the edit button (subscript is 0 ), there is a space (subscript: 1) between the edit button and the delete button)
Linkbutton lbtn = (linkbutton) E. Row. cells [6]. controls [2];

// Determine whether the obtained control name is delete. To prevent the update cancle button from being bound to a pop-up window after you click Edit
If (lbtn. commandname = "delete ")
{
Lbtn. onclientclick = "Return confirm ('Do you really want to delete? ');";
}
}

}

4. gridview Edit

// Click Edit to edit the row.

Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)

{
Gridview1.editindex = E. neweditindex;

// Databind is the function used to bind the gridview data source.
Databind ();
}

// Update the value after editing a row to the database

Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
Int id = convert. toint32 (gridview1.datakeys [E. rowindex]. value. tostring ());
String name = (textbox) gridview1.rows [E. rowindex]. cells [1]. controls [0]). Text. tostring (). Trim ();
...

Int flag = updatedatabase ();
If (flag = 0)
{
Page. clientscript. registerstartupscript (this. GetType (), "alert", "<SCRIPT> alert ('updated successfully! '); </SCRIPT> ");
}
Else
{
Page. clientscript. registerstartupscript (this. GetType (), "alert", "<SCRIPT> alert ('failed' to update! '); </SCRIPT> ");
}

Gridview1.editindex =-1;
Databind ();
}

// Cancel editing

Protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs E)

 {
Gridview1.editindex =-1;
Databind ();
}

5. gridview Delete

Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
String id = gridview1.datakeys [E. rowindex]. value. tostring ();
Int flag = deletefromdatabase ();
If (flag = 0)
{
Page. clientscript. registerstartupscript (this. GetType (), "alert", "<SCRIPT> alert ('deletedsuccessfully! '); </SCRIPT> ");
}
Else
{
Page. clientscript. registerstartupscript (this. GetType (), "alert", "<SCRIPT> alert ('failed' to delete! '); </SCRIPT> ");
}

Databind ();
}

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.