Delete and update a Custom button in the gridview

Source: Internet
Author: User

I used a stupid method. After reading it for two hours, I figured it out.
Rowcommand was used for deletion and update. Later, we found that the value in the Custom input box could not be obtained in rowcommand, so we had to change the update to rowupdating, however, rowupdating does not have the commandargument attribute, so you have to put a hiddenfield for Parameter Storage. It's helpless. I don't know how to use it. Can you give me some advice?
How to operate data in the gridview:
1. Delete:

Create a button in the template and set its commandtype = "Del", commandargument = "<% # eval (" ID ") %>"
Determine commandtype in the rowcommand event of the gridview and delete data with commandargument as the parameter

2. Update:

Create a button in the template and set its commandtype = "Update"
Create a hiddenfield and set it to value = "<% # eval (" ID ") %>"
In the rowupdating event of the gridview, first use E. rowindex to find the index of the current row, and then use findcontrol to get the value and update it.

3. Update 2:

Another method is to write <% # (gridviewrow). rowindex %> In commandargument of the "edit" button.
In the rowcommand event of the gridview, you can use commandargument to obtain the index of the current row and then process it.
BelowCodeIs not implemented using this method

Protected   Void Gridview1_rowcommand ( Object Sender, gridviewcommandeventargs E)
{
If (E. commandname =   " Del " )
{
Int Index = Convert. toint32 (E. commandargument );
String Sqlcmd =   " Delete from Admin where id = "   + Index;
Usersql. DB. executenonquery (sqlcmd );
Databinded ();
}
}
Protected   Void Gridview1_rowupdating ( Object Sender, gridviewupdateeventargs E)
{
Int Index2 = E. rowindex;
String Txtname = (Textbox) This . Gridview1.rows [index2]. cells [ 2 ]. Findcontrol ( " Txtadminname " ). Text;
String Txtpass = (Textbox) This . Gridview1.rows [index2]. cells [ 2 ]. Findcontrol ( " Txtadminpassword " ). Text;
Int ID = Convert. toint32 (hiddenfield) This . Gridview1.rows [index2]. cells [ 2 ]. Findcontrol ( " Hiddenid " ). Value );
String Sqlcmd =   " Update [admin] Set [adminname] = @ adminname, [adminpassword] = @ adminpassowrd where [ID] = @ ID " ;
Sqlparameter [] Param =   {
New Sqlparameter ( " @ Adminname " , Txtname ),
New Sqlparameter ( " @ Adminpassowrd " , Txtpass ),
New Sqlparameter ( " @ ID " , ID ),
} ;
Usersql. DB. executenonquery (sqlcmd, Param );
Databinded ();
}

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.