Add the delete button to the record in the gridview Control

Source: Internet
Author: User

After the data binding of the gridview control is complete, how can I add the delete button to the record? After searching for the record for a long time on the Internet, I finally got it done. It's actually very simple, but I am stupid,
Here is :()

1.Drag the gridview control into the aspx file:

1 < ASP: gridview ID = " Grid1 " Runat = " Server " Cellpadding = " 4 " Forecolor = " #333333 " Gridlines = " None "   Datakeynames ="ID"   Autogeneratecolumns ="False"   Onrowdeleting = " Grid1_deleterow " >
2 < Columns >
3 < ASP: boundfield headertext = " ID " Datafield = " ID "   />
4 < ASP: boundfield headertext = " Username " Datafield = " Username "   />
5 < ASP: boundfield headertext = " PWD " Datafield = " PWD "   />
6 < ASP: commandfield Showdeletebutton ="True " />
7 </ Columns >
8 </ ASP: gridview >

Note that the datakeynames attribute of the black text must be available; otherwise, the key value of the current record cannot be obtained during deletion (as I understand ).
Autogeneratecolumns = " False "This setting prevents the gridview control from automatically generating columns, which are manually generated by <columns> below. In this way, you can put the" delete "column after the record. Of course, it can also be automatically generated, but it seems that the "delete" column is always before the record. that's ugly!
When the showdeletebutton attribute is set to true, the delete button will appear. Of course, you have to write the deletion event yourself and set it in the onrowdeleting attribute.
2. In the corresponding. CS file: (The gridview is omitted. As I mentioned in the previous article, only the deletion event is written here) 1 Protected   Void Grid1_deleterow ( Object Sender, gridviewdeleteeventargs E)
2 {
3 String ID =  Grid1.datakeys [E. rowindex]. value. tostring ();
4 String SQL =   " Delete from demo where id = "   + ID;
5
6 Conn =   New Sqlconnection ( " Server = localhost; database = demo; uid = sa; Pwd = 123456 " );
7 Conn. open ();
8 Comm. Connection = Conn;
9 Comm. commandtext = SQL;
10 Comm. executenonquery ();
11
12 Response. Redirect (request. url. tostring (); // do not forget to refresh the page after the deletion. Otherwise, try again!
13 }

The premise that the black text part can run is the datakeynames attribute in the aspx File above. If it is not set here, an error will be reported.
OK, it's detailed enough, because I just got in touch with. net, so the details of the record are too detailed and too many bytes ~~~

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.