0

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?

user4221591
  • 2,084
  • 7
  • 34
  • 68
  • Try using `confirm` javascript code on delete button, without calling server side function, here is the code: https://stackoverflow.com/a/14058202/1662459 – G J Aug 21 '17 at 09:39
  • Still the same problem. The code behind method `GridView1_RowDeleting` can not be called. – user4221591 Aug 21 '17 at 15:40
  • you can look at this: https://stackoverflow.com/a/7062352/1662459 – G J Aug 21 '17 at 16:02
  • This have a demo too, https://www.aspsnippets.com/Articles/Delete-ASPNet-GridView-Row-with-JavaScript-Confirmation-Box-using-CommandField-and-OnRowDeleting-event.aspx – G J Aug 21 '17 at 16:05

0 Answers0