Grideview (ii)---delete function

Source: Internet
Author: User

Scenario one, no foreign key association,
Action: Remove options in the data source Select---The delete option in Grideview
Scenario Two, there are foreign key associations
*rowdeleting trigger before the row is deleted
*rowdeleted triggered after a row has been deleted
The operation has a foreign key associated with the table, you need to delete the associated data before performing the deletion:
Law One:
Perform a delete operation before performing

1 protected voidGridview1_rowdeleting (Objectsender, Gridviewdeleteeventargs e)2 {3 ////Get PRIMARY key4 //string key = e.keys["Code"]. ToString (); //There is a premise that the DataKeyNames attribute of the GridView must be guaranteed to have a value beforehand. 5 // //Perform the delete6 //var query1 = _context.work.where (p = = P.infocode = = key);7 //var query2 = _context.family.where (P=>p.infocode = = key);8 //_context.work.deleteallonsubmit (query1);9 //_context.family.deleteallonsubmit (query2);Ten //_context.submitchanges (); One ////Perform LinqDataSource delete function A } - //after the deletion is done, the user is prompted to delete the complete - protected voidGridview1_rowdeleted (Objectsender, Gridviewdeletedeventargs e) the { - //literal1.text = "<script language=javascript>alert (' deletion succeeded ') </script>"; -}

Query data related data, when no data, page tips
Set the Emptydatatext property to: "No related data found!" ”

Law II:

1 //Delete Work resumes and family relationships2 protected voidGridview1_rowdeleting (Objectsender, Gridviewdeleteeventargs e)3 {4 //Get primary Key5 stringKey = e.keys["Code"]. ToString ();6 //Delete all three of the watches. 7 varquery = _context.info.where (P=>p.code = =key);8Info data =query. First ();9 Ten _context.work.deleteallonsubmit (data. work); One _context.family.deleteallonsubmit (data. Family); A _context.info.deleteonsubmit (data); - _context.submitchanges (); - //Refresh to prevent the event from moving forward.  the Gridview1.databind (); -E.cancel =true;//block the sequence of events.  - //literal1.text = "<script language=javascript>alert (' deletion succeeded ') </script>";//Prompt for deletion success -}

Method Three, 1, edit column--Add Hyperlink column (HyperLinkField)--Set text = "Delete";
2. Point this hyperlink to a field (Datanavigateurlfields=code)
3, datanavigateurlformatstring= "delete.ashx?id={0}" Note: {0} means the above point to the code;
4, add the general handler (DELETE.ASHX) to add the removal program and jump back to the display page;


Scenario Description: When you perform a delete, you are prompted to confirm the deletion
Law I,

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e) {//Give Delete button plus confirm if (E.row.rowtype = = Datacontrolrowtype.datarow) {LinkButton btndelete = e.row.cells[5]. Controls[0] as Linkbutton;btndelete.onclientclick = "return confirm (' Confirm to delete? ‘)";}}

  


Add style to delete in edit column, add style after
Add a click event via jquery
<script language= "JavaScript" >
$ (document). Ready (function () {
$ (". Del"). Click (function () {
return confirm (' Confirm to delete? ‘);
});
});
</script>

Grideview (ii)---delete function

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.