ASP.NET - confirmation box

Asked By rajesh kokul on 04-Jun-12 10:49 AM
I have written code for confirmation box on gridview rowdatabound,it works fine exept after cancle delete in confirmation box one of gridview column gets hide..

Here is my code..

 foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    
                    foreach (Control control in cell.Controls)
                    {
                        
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                           
                  button.OnClientClick = "if(!confirm('This action will DELETE selected Batch.Are you sure ? " + "'))return;";
                    }
                }
dipa ahuja replied to rajesh kokul on 04-Jun-12 11:13 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();
  }
}
 
 
kalpana aparnathi replied to rajesh kokul on 04-Jun-12 02:22 PM
hi,

Do you check for the ispostback() condition for the gridview?if not try like this.

If(!ispostback())
{
//code for binding gridview

}

Regards,
DL M replied to rajesh kokul on 05-Jun-12 12:25 AM
To adding a confirmation box to each row when the rows are bound.Add a reference to an event handler in the GridView

OnRowDataBound=”gridOverview_OnRowDataBound”

    The delete column (ItemTemplate) should be an (image) button in a template field (replace the squared area in the example above with this code):

      <asp:TemplateField HeaderText=”Delete” >
        <ItemStyle CssClass=”grid-column-buttons” />
        <ItemTemplate>
          <asp:ImageButton ID=”ibDelete” runat=”server”
            CausesValidation=”False” CommandName=”Delete”
            ImageUrl=”~/Images/icon-delete.gif” />
        </ItemTemplate>
      </asp:TemplateField>

->Create the event handler ‘gridOverview_OnRowDataBound’ in the code behind. When each row is bound, this procedure will add a message box to the image button.

protected void gridOverview_OnRowDataBound(object sender, GridViewRowEventArgs e)
//Add delete msgbox to each row containing field
//information to identify the row
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      // reference the Delete ImageButton
      ImageButton ib = (ImageButton)e.Row.FindControl(“ibDelete”);
      ib.Attributes.Add(“onclick”,“javascript:return ” +

        confirm(‘Are you sure about deleting ” +
        DataBinder.Eval(e.Row.DataItem, “bookTitle”) + “?’);”);
    }
}
TSN ... replied to rajesh kokul on 05-Jun-12 12:49 AM

Follow the steps.

First : Add a Link Button with the Command Name “Delete”

<ItemTemplate><asp:LinkButtonID="LinkButton1"CommandArgument='<%# Eval("ID") %>'CommandName="Delete"runat="server">Delete</asp:LinkButton></ItemTemplate>

Second :Add the Below code uin the RowDatabound Event to bind the event on click for the Link Button

 

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

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

{

LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");

lb.Attributes.Add("onclick", "javascript:return " +

"confirm('Are you sure you want to delete this record " +

DataBinder.Eval(e.Row.DataItem, "ID") + "')");

}

}

Now add the code to delete in the RowCommand Event

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "Delete")

{

int ID = Convert.ToInt32(e.CommandArgument); // get the ID of the clicked row

// Delete the record

DeleteRecordByID(ID); // Implement this on your own :)

}

}

Jitendra Faye replied to rajesh kokul on 05-Jun-12 01:13 AM
You can simply add confirmation code like this-

<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 article-

http://steptodotnet.blogspot.in/2012/01/delete-confirmation-in-gridview.html?z#!