I am trying to show confirm message to the user on pressing delete button of every row.
Initially I tried using <asp:CommandField ShowDeleteButton="true" ButtonType="Button" />
but the problem was I can not show the confirmation message.
Then I tried using asp:TemplateField
for the button. But the problem is asp:TemplateField
can not call OnRowDeleting Method but I get the confirmation message. The code behind method on pressing the delete button is not executed, OnRowDeleting="GridView1_RowDeleting"
. Why is it so?
asp code::
<asp:GridView
ID="GridView1"
runat="server"
Font-Size="11px"
AutoGenerateColumns="False"
Width="90%" CssClass="gridview" AlternatingRowStyle-CssClass="even"
DataKeyNames="ID"
OnRowDeleting="GridView1_RowDeleting"
HeaderStyle-HorizontalAlign="Left">
<Columns>
<asp:TemplateField HeaderText = "S No.">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField
DataField="ID" Visible="false">
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField
DataField="PATIENT ID" HeaderText="PATIENT ID">
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:BoundField
DataField="PATIENT NAME" HeaderText="PATIENT NAME">
<ItemStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:CommandField ShowDeleteButton="true" ButtonType="Button" />
<%--<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button runat="server" Text="Delete"
CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this event?');"
AlternateText="Delete" />
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
</asp:GridView>
aspx code
protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
try
{
string result = string.Empty;
int PID_CANCELLATION_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
result = new DAL_PID_Cancellation().set_PID_Cancelled_Flag(PID_CANCELLATION_ID, Session["username"].ToString());
if(result == "1")
{
get_Cancel_PID();
return;
}
else
{
get_Cancel_PID();
return;
}
}
catch (Exception) { throw; }
}
How can I call OnRowDeleting
Method using asp:TemplateField
and also show the confirmation message on delete button press?