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" style="font-size:12px;">
                            <tr>
                            <td style="text-align: right">
                                第<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>页  
                              <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;";
            }
        }

时间: 2024-10-31 19:19:03

GridView自定义删除操作的具体方法_实用技巧的相关文章

asp.net GridView中使用RadioButton单选按钮的方法_实用技巧

本文实例讲述了asp.net GridView中使用RadioButton单选按钮的方法.分享给大家供大家参考,具体如下: 在GridView里做单选按钮,我用了三种方法 第一种方法:在GridView的模版列里加服务器端控件RadioButton,使用js控制单选 使用模版列里加RadioButton <script type="text/javascript"> function setRadio(nowRadio) { var myForm,objRadio; myF

根据Eval()函数绑定的值,来显示GridView中的控件的方法_实用技巧

复制代码 代码如下: <asp:TemplateField HeaderText="操作" ShowHeader="False">                                 <ItemTemplate>                                 <asp:LinkButton ID="btn_zhiding" runat="server" Command

10个.NET中删除空白字符串的方法_实用技巧

我们有无数方法可用于删除字符串中的所有空白,但是哪个更快呢? 介绍 如果你问空白是什么,那说起来还真是有些乱.许多人认为空白就是SPACE 字符(UnicodeU+0020,ASCII 32,HTML ),但它实际上还包括使得版式水平和垂直出现空格的所有字符.事实上,这是一整类定义为Unicode字符数据库的字符. 本文所说的空白,不但指的是它的正确定义,同时也包括string.Replace(" ", "")方法. 这里的基准方法,将删除所有头尾和中间的空白.这就

弹出窗口,点击确定在删除数据的实现方法_实用技巧

复制代码 代码如下: <head runat="server">     <title></title>     <script type="text/javascript">         function myClick() {             if (confirm("你確定要刪除嗎?")) {                 return true;             }  

gridview调整单元格宽度的方法_实用技巧

<asp:BoundField DataField="Comments" HeaderText="评论"> <ItemStyle width="300px" /> </asp:BoundField>

asp.net 删除,更新数据库方法_实用技巧

复制代码 代码如下: '数据更新 Public Sub updateTable(ByVal StrSql As String) objCommand.CommandText = StrSql Try conSql.Open() Trans = conSql.BeginTransaction objCommand.Transaction = Trans objCommand.ExecuteNonQuery() Trans.Commit() Catch ese As Exception MsgBox

GridView使用CommandField删除列实现删除时提示确认框_实用技巧

GridView在使用CommandField删除时弹出提示框,在.net2005提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" />,完后在它的RowDeleting事件中完成删除.但在多半我们在做这种删除操作时都需要先让操作者再确认下,完后再进行删除,以避免误操作引起的误删除. 可以通过下面方法给GridView删除前加上个确认对话框. 首先,在GridVie

GridView中日期不显示时分秒的完美解决方法_实用技巧

两种处理方式: 1.模版列:假设数据表的字段completeTime的类型为时间格式 <asp:TemplateField HeaderText="时间"> <ItemTemplate> <%#Eval("completeTime", "{0:yyyy-MM-dd}")%> </ItemTemplate> </asp:TemplateField> 2.绑定列: <asp:Bound

asp.net C#检查URL是否有效的方法_实用技巧

我们有时候需要对用户输入的网站(URL)进行有效性检查, 复制代码 代码如下: function CheckUrl(str) {    var RegUrl = new RegExp();    RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&?/.=]+$");    if (!RegUrl.test(str)) {        return false;    }    return true;} 不止