Use ASP. Net to delete a row using the GridView Template

Source: Internet
Author: User

Add a GridView (after binding a data source to it)-> (on the design page) edit-

> Add template-> set attributes of the column template-> return

-> Edit template-> Add a Linkbutton-> set Text to delete

> Set the CommandName attribute (for example, CommandName = "Delete ")

-> Set the CommandArgument value {for example, CommandArgument = '<% # Bind ("stuNO ")}

> Add the RowCommand event of the GridView.

Code:

Webpage Design Code:

<Asp: TemplateField ShowHeader = "False">
<ItemTemplate>
<Asp: LinkButton ID = "LinkButton1" runat = "server" CausesValidation = "False"
CommandName = "Delete" Text = "Delete" OnClick = "return confirm ('Do you want to Delete data? ') "CommandArgument =' <% # Bind (" stuNO ") %> '> </asp: LinkButton>
</ItemTemplate>
</Asp: TemplateField>

Main background code:

Protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e)
{
If (e. CommandName = "Delete") // if the button is deleted
{
String stuNO = e. CommandArgument. ToString (); // obtain the student ID.

Int result = stuBll. delStudent (stuNO); // use a three-tier architecture to call the method at the business logic layer.
If (result> 0)

{

Response. Write ("deleted successfully! ");

}
Else
{
Response. Write ("deletion failed! ");
}

GvBind (); // rebind data after deletion
}
}

 


Author: ruiying

Related Article

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.