使用asp:TemplateField或asp:CommandField调用OnRowDeleting方法

时间:2017-08-21 09:32:58

标签: javascript c# asp.net

我试图在按下每行的删除按钮时向用户显示确认消息。 最初我尝试使用<asp:CommandField ShowDeleteButton="true" ButtonType="Button" /> 但问题是我无法显示确认信息。

然后我尝试使用asp:TemplateField作为按钮。但问题是asp:TemplateField无法调用OnRowDeleting方法但我收到确认消息。按下删除按钮的代码方法不会执行OnRowDeleting="GridView1_RowDeleting"。为什么会这样?

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代码

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; }
        }

如何使用OnRowDeleting拨打asp:TemplateField方法,并在按下删除按钮时显示确认消息?

0 个答案:

没有答案