ASP.NET - Row deleting event not getting fired after confirmation javascript

Asked By anbu n on 28-Mar-12 11:08 AM
design:

<asp:GridView ID="gvGeneralMasterdtls" runat="server"
OnRowDeleting="gvGeneralMasterdtls_OnRowDeleting"
>

<Columns>
<asp:BoundField DataField="Master_Static_Id" HeaderText="Module_id" />
<asp:BoundField DataField="_Date" HeaderStyle-Width="5%" HeaderText="Date" ItemStyle-Width="5%"
SortExpression="_Date" />
<asp:CommandField CausesValidation="false" ShowEditButton="True" ButtonType="Image" HeaderStyle-Width="1%"
ItemStyle-Width="1%" EditImageUrl="~/Images/Control_Style/Edit01.png" EditText="Edit"
ControlStyle-Height="20px" />
<asp:CommandField CausesValidation="false" ShowDeleteButton="True" ButtonType="Image" DeleteText="Delete" HeaderStyle-Width="1%"
ItemStyle-Width="1%" DeleteImageUrl="~/Images/Control_Style/delete.png" ControlStyle-Height="20px" />
</Columns>
</asp:GridView>
-------------------------------------------------------------------
javascript :

function Confirmation()
{
if (confirm('Are you sure want to delete?'))
{
return true;
}
else
{
return false;
}
}


//Table confirm validatation
function setconformation()
{
var PlaceHolder = 'ctl00_ContentPlaceHolder1_';

var tbl=null;

tbl = document.getElementById(PlaceHolder + 'gvGeneralMasterdtls');

if(tbl!=null)

for(var i=1;i<tbl.rows.length;i++)
{
if(tbl.rows[i].cells[5] == null) continue;

var anchor = tbl.rows[i].cells[5].getElementsByTagName('input')[0];

anchor.onclick=Confirmation;

}
}
---------------------------------------------------------------------
Code behind:

protected void gvGeneralMasterdtls_OnRowDeleting(Object sender, GridViewDeleteEventArgs e)
{
try
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "setconformation();", true);

Setting_Master _ob_enty = new Setting_Master();

_ob_enty._Id = Convert.ToInt32(gvGeneralMasterdtls.DataKeys[e.RowIndex].Value);
_ob_enty._Name = ddlModule.SelectedItem.Text;
Master_general_service.Delete(_ob_enty, false);

BindGvModuleContents(Convert.ToInt32(ddlModule.SelectedValue), ddlModule.SelectedItem.Text);
}
catch (Exception ex)
{
}
}
---------------------

In the above grid row deleting is not getting fired after , I have added a confirmation java script to it.


D Company replied to anbu n on 28-Mar-12 12:48 PM
it seems that the event is not getting fired, first thing request u to put the breakpoint and see if the event is firing, and if not then for delete the row use link button instead of imagebutton and surely once u click ok on  the Javascripft confirmation window it will fire the event , let us know if still it does not work.

Regards
D
kalpana aparnathi replied to anbu n on 28-Mar-12 01:57 PM
hi,

Please try confirmation message in onclientclick() it would helps to solve your problem.

Regards,
dipa ahuja replied to anbu n on 28-Mar-12 03:35 PM
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();
  }
}
 
 
Somesh Yadav replied to anbu n on 29-Mar-12 08:26 AM
Hi

 Replace  Your command field for delete button with the following, and remove  all othe javascript.

 <asp:TemplateField >
 
<ItemTemplate >
 
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete"
 
ImageUrl="~/Images/Control_Style/delete.png"
 
OnClientClick="return confirm('Are you sure you want to delete this data Row?')" />
 
</ItemTemplate>
 
</asp:TemplateField>