ASP.NET - Gridview Delete Confirmation Using An Link Button In A CommandField?

Asked By KCH on 19-Jun-12 04:54 AM
Hi,
Please help me....Thank u
Jitendra Faye replied to KCH on 19-Jun-12 04:56 AM
use this code-


<ItemTemplate>
<asp:Button ID="btnDelate" runat="server" onclick="btnDelete_Click" OnClientClick="return confirm('Are you certain you want to delete this product?');" Text="Delete" />
<ItenTemplate>



refer this link-=

http://steptodotnet.blogspot.in/2012/01/delete-confirmation-in-gridview.html
S K replied to KCH on 19-Jun-12 05:13 AM
See this thread having good example with complete code how can use confirmation messages on delete link in gridview
http://www.codeproject.com/Articles/32756/ASP-NET-GridView-delete-confirmation-using-asp-Com 
KCH replied to Jitendra Faye on 19-Jun-12 05:37 AM
Hi vickey,
i have used command field not button.how can i do for that.
check here

 <Columns>
   
<asp:CommandField  DeleteText="Delete" ShowDeleteButton="true" ButtonType="Link"  />

    <asp:CommandField ButtonType="Link" EditText="Edit" ShowEditButton="true"  />
    <asp:BoundField DataField="UserName" HeaderText="UserName" />
    <asp:BoundField DataField="Email" HeaderText="Email" />
    <asp:BoundField DataField="MobilePIN" HeaderText="MobilePIN" />
    </Columns>
Chintan Vaghela replied to KCH on 19-Jun-12 06:43 AM

Hi Frndz,

 

Functionality:  Command Field Delete Link Display Confirm Message

 

To achieve this task,

 

Find Delete Link on RowDataBound Event ,

 

            LinkButton deleteButton = (LinkButton)e.Row.Cells[0].Controls[0];

 

 

And assign it to onclick javascript attributes.

 

            deleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this item?');");

 

 

Note :: In following example CommanField set as First Column.

 

 

 

Logic   :

 

 

<asp:CommandField HeaderText="Action" ShowDeleteButton="true" />

 

protected void GrvMfDetails_RowDataBound(object sender, GridViewRowEventArgs e)

    {

      if (e.Row.RowType == DataControlRowType.DataRow)

      {

        if (e.Row.Cells[0] != null)

        {

          if (e.Row.Cells[0].HasControls())

          {

            LinkButton deleteButton = (LinkButton)e.Row.Cells[0].Controls[0];

            deleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this item?');");

 

          }

        }

      }

}

 

Hope this helpful!

Thanks

 

 

 

Jitendra Faye replied to KCH on 19-Jun-12 06:51 AM
Refer this link-

http://www.codeproject.com/Articles/20360/Extending-the-GridView-CommandField-To-Add-Delete

Here you will get example.
dipa ahuja replied to KCH on 19-Jun-12 09:10 AM
Hi here is the Simple Solution to Implement:
 
Add a linkButton inside the Item Template in Gridview
 
<asp:LinkButton ID="lnkDelete" CommandName="Delete" Text="Delete" runat="server"/>
 
 
Add javascript for linkButton in RowDataBound Event:
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
      l.Attributes.Add("onclick", "javascript:return " +
      "confirm('Are you sure you want to delete this record " +
      DataBinder.Eval(e.Row.DataItem, "id") + "')");
    }
  }
 
Implement the RowCommand  Event
 
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{   
  if (e.CommandName == "Delete")
  {
  GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
  int rowIndex = int.Parse(row.RowIndex.ToString());
    string id = ((Label)row.FindControl("lblid")).Text;
    
  SqlConnection connect = new SqlConnection(ConnString);
  connect.Open();
 
  string q = "Delete people where id=@id";
 
  SqlCommand comm = new SqlCommand(q, connect);
  comm.Parameters.AddWithValue("id", id);
  comm.ExecuteNonQuery();
  connect.Close();
 
  GridView1.EditIndex = -1;
  //BindGrid();
  }
}