GridView custom delete operation of the specific method

  • 2020-06-23 00:09:27
  • OfStack

First of all, our front-end code is as follows:


<asp:GridView ID="gridViewDxjk" CssClass="gridview" runat="server" AllowPaging="True" 
                  DataKeyNames="P_ID" AutoGenerateColumns="False"  
                      RowStyle-HorizontalAlign="Center" BorderWidth="1px" PageSize="17" 
                      onrowdeleting="gridViewDxjk_RowDeleting" 
                      OnRowDataBound="gridViewDxjk_RowDataBound" 
                      onpageindexchanging="gridViewDxjk_PageIndexChanging" >
                        <HeaderStyle CssClass="head" />
                        <PagerStyle CssClass="pager" />
                        <RowStyle CssClass="row" />
                        <EditRowStyle CssClass="editrow" />
                        <AlternatingRowStyle CssClass="altrow" />
                        <EmptyDataRowStyle CssClass="empty" />
                        <Columns>                            
                         <asp:HyperLinkField HeaderText=" The editor " ControlStyle-Width="50" DataNavigateUrlFields="P_ID" DataNavigateUrlFormatString="smsModify.aspx?id={0}" Text=" The editor "  >
                                <ControlStyle Width="50px"></ControlStyle></asp:HyperLinkField>
                                <asp:CommandField  ShowDeleteButton="true" DeleteText=" delete "   >
                                <ControlStyle Width="50px"></ControlStyle></asp:CommandField>
                                <asp:BoundField DataField="P_ID" HeaderText="id" SortExpression="P_ID" ItemStyle-HorizontalAlign="Center"  Visible="False" > 
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_NAME" HeaderText=" The name of the " SortExpression="P_NAME" />
                                <asp:BoundField DataField="P_Type" HeaderText=" Inform the way " SortExpression="P_Type" ItemStyle-HorizontalAlign="Center"  > 
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_Fzr" HeaderText=" The name " SortExpression="P_Fzr" ItemStyle-HorizontalAlign="Center"  > 
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_tel" HeaderText=" Notify the phone " SortExpression="P_tel" ItemStyle-HorizontalAlign="Center"  > 
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_jg" HeaderText=" Notify the interval ( hours )" SortExpression="P_jg" ItemStyle-HorizontalAlign="Center"  > 
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_on" HeaderText=" Whether open " SortExpression="P_on" ItemStyle-HorizontalAlign="Center"  > 
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_lasttime" HeaderText=" Last sending time " SortExpression="P_lasttime" ItemStyle-HorizontalAlign="Center"  > 
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_memo" HeaderText=" note " SortExpression="P_memo" ItemStyle-HorizontalAlign="Center"  >                   
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                        </Columns>
                        <EmptyDataTemplate>
                         No data! 
                        </EmptyDataTemplate>
                         <PagerTemplate>
                        <table width="100%" class="gvPage" style="font-size:12px;">
                            <tr>
                            <td style="text-align: right">
                                 The first <asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'></asp:Label> page 
                                / A total of <asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label> page &nbsp;&nbsp;
                              <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"> Home page </asp:LinkButton>
                              <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"  Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"> on 1 page </asp:LinkButton>
                              <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"  Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>"> Under the 1 page </asp:LinkButton>
                              <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"  Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>"> back </asp:LinkButton>
                              <asp:TextBox ID="txtNewPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'  Width="20px" AutoPostBack="true" ></asp:TextBox>
                              <asp:LinkButton ID="btnGoEx" runat="server" CommandArgument="GO" CommandName="Page" Text="GO" OnClick="btnGoEx_Click"></asp:LinkButton>
                            </td>
                            </tr>
                        </table>
                    </PagerTemplate>
                 </asp:GridView>

In the back end, we need to pop up the confirmation box before deleting, so what do we need to do in RowDataBound? At the same time, to actually delete, you need to trigger the RowDeleting event as follows:


// Alarm delete 
        protected void gridViewDxjk_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string key = gridViewDxjk.DataKeys[e.RowIndex].Value.ToString();
            bool flag = bll.Delete(Int32.Parse(key));
            if (flag)
                NXT_WLService.App_Code.JScript.Alert(" Delete successful! ", this);
            else
                NXT_WLService.App_Code.JScript.Alert(" Delete failed! ", this);
        }
        
        protected void gridViewDxjk_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btn = (LinkButton)e.Row.Cells[1].Controls[0];
                if (btn.Text.Equals(" delete "))// Information is added only when the message is divided   
                    btn.OnClientClick = "if (confirm(' You are sure to delete ?')) javascript:__doPostBack('gridViewDxjk','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;";
            }
        }


Related articles: