温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

GridView中怎么自定义删除操作

发布时间:2021-07-27 15:27:04 来源:亿速云 阅读:125 作者:Leah 栏目:开发技术

本篇文章为大家展示了GridView中怎么自定义删除操作,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

首先,我们前端的代码如下:


<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="编辑" ControlStyle-Width="50" DataNavigateUrlFields="P_ID" DataNavigateUrlFormatString="smsModify.aspx?id={0}" Text="编辑"  >
                                <ControlStyle Width="50px"></ControlStyle></asp:HyperLinkField>
                                <asp:CommandField  ShowDeleteButton="true" DeleteText="删除"   >
                                <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="名称" SortExpression="P_NAME" />
                                <asp:BoundField DataField="P_Type" HeaderText="通知方式" SortExpression="P_Type" ItemStyle-HorizontalAlign="Center"  >
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_Fzr" HeaderText="姓名" SortExpression="P_Fzr" ItemStyle-HorizontalAlign="Center"  >
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_tel" HeaderText="通知手机" SortExpression="P_tel" ItemStyle-HorizontalAlign="Center"  >
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_jg" HeaderText="通知间隔(小时)" SortExpression="P_jg" ItemStyle-HorizontalAlign="Center"  >
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_on" HeaderText="是否开启" SortExpression="P_on" ItemStyle-HorizontalAlign="Center"  >
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_lasttime" HeaderText="最后发送时间" SortExpression="P_lasttime" ItemStyle-HorizontalAlign="Center"  >
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                                <asp:BoundField DataField="P_memo" HeaderText="备注" SortExpression="P_memo" ItemStyle-HorizontalAlign="Center"  >                  
                                <ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
                        </Columns>
                        <EmptyDataTemplate>
                        没有数据!
                        </EmptyDataTemplate>
                         <PagerTemplate>
                        <table width="100%" class="gvPage" >
                            <tr>
                            <td >
                                第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'></asp:Label>页
                                /共<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label>页&nbsp;&nbsp;
                              <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
                              <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"  Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
                              <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"  Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
                              <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"  Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</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>


后端的话,由于需要弹出删除前的确认框,所以,我们需要在RowDataBound里面做点什么?同时,要想真正的删除,还需要触发RowDeleting事件,具体代码如下:

复制代码 代码如下:


//报警删除
        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("删除成功!", this);
            else
                NXT_WLService.App_Code.JScript.Alert("删除失败!", 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("删除"))//刪除鈕才加提示訊息 
                    btn.OnClientClick = "if (confirm('你确认要删除?')) javascript:__doPostBack('gridViewDxjk','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;";
            }
        }

上述内容就是GridView中怎么自定义删除操作,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI