The Declarative ApproachWhile using TemplateField, we can directly use the OnClientClick event of the LinkButton Server control and call JavaScript confirm function, as given below.
- <ItemTemplate>
- <asp:LinkButtonID="lnkDelete"runat="server"
- OnClick="lnkDelete _Click" OnClientClick="return confirmOnDelete('');">Delete</asp:LinkButton>
- </ItemTemplate>
The Code behind ApproachThe Code Behind approach is almost the same, as we did using the RowDataBound event to access the ShowDeleteButton. The output is given below.
- protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowState != DataControlRowState.Edit)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- string id = gvEmployee.DataKeys[e.Row.RowIndex].Value.ToString();
-
- LinkButton lb = (LinkButton)e.Row.Cells[4].FindControl("lnkDelete");
- if (lb != null)
- {
-
- lb.Attributes.Add("Onclick", "return confirmOnDelete('" + id + "');");
- }
- }
- }
- }
It shows as error