Priyanka Singh

Priyanka Singh

  • 655
  • 1.4k
  • 624.1k

Error while deletion of data in gridview.

May 1 2019 4:20 AM
The Declarative Approach
While using TemplateField, we can directly use the OnClientClick event of the LinkButton Server control and call JavaScript confirm function, as given below.
  1. <ItemTemplate>  
  2. <asp:LinkButtonID="lnkDelete"runat="server"  
  3. OnClick="lnkDelete _Click" OnClientClick="return confirmOnDelete('');">Delete</asp:LinkButton>  
  4. </ItemTemplate> 
The Code behind Approach
The Code Behind approach is almost the same, as we did using the RowDataBound event to access the ShowDeleteButton. The output is given below.
  1. protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.        {  
  3.            if (e.Row.RowState != DataControlRowState.Edit) // check for RowState  
  4.            {  
  5.                if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType  
  6.                {  
  7.                    string id = gvEmployee.DataKeys[e.Row.RowIndex].Value.ToString();  
  8.                    //cast the ShowDeleteButton link to linkbutton  
  9.                    LinkButton lb = (LinkButton)e.Row.Cells[4].FindControl("lnkDelete");  
  10.                    if (lb != null)  
  11.                    {  
  12.                    //attach the JavaScript function and pass the ID as the paramter  
  13.                        lb.Attributes.Add("Onclick""return confirmOnDelete('" + id + "');");  
  14.                    }  
  15.                }  
  16.            }  
  17.        } 
It shows as error
 
 

Answers (7)