Home
Manage Your Code
Snippet: Link Button in Gridview (C#)
Title: Link Button in Gridview Language: C#
Description: Tie LinkButton to Gridview Delete CommandName Views: 746
Author: David Ashworth Date Added: 10/7/2007
Copy Code  
1<div style='margin:5px;background-color:#B81436;padding: 2px;border-color: Black; border-width:1px; text-decoration: underline'><asp:LinkButton ID="LinkButton1" 
2         CommandArgument='<%# Eval("ProductImageID") %>'  CssClass="menu" Font-Bold="true"
3         CommandName="Delete" runat="server">Delete</asp:LinkButton></div>
4
5//Method for prompt
6protected void gvImages_RowDataBound(object sender, GridViewRowEventArgs e)
7    {
8        if (e.Row.RowType == DataControlRowType.DataRow)
9        {
10            LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
11            l.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this image?');");
12        }
13    }
14
15//method for detecting Delete Command event
16 protected void gvImages_RowCommand(object sender, GridViewCommandEventArgs e)
17    {
18        if (e.CommandName == "Delete")
19        {
20            // get the categoryID of the clicked row
21            int productImageID = Convert.ToInt32(e.CommandArgument);
22
23            // Delete the record 
24            try
25            {
26                CYRDAL cDal = new CYRDAL();
27                cDal.DeleteProductImage(productImageID);
28
29                //get the product images
30                DataSet di = cDal.SelectImagesByProductID(int.Parse(Request.QueryString["ProductID"].ToString()));
31                gvImages.DataSource = di;
32                gvImages.DataBind();
33            }
34            catch (Exception)
35            {
36                throw;
37            }
38        }
39    }
40