zoukankan      html  css  js  c++  java
  • GridView 删除记录的处理提示

    在gridview中,我们都希望能在删除记录时,能弹出提示框予以提示,在asp.net 1.1中,都可以很容易实现,那么在asp.net 2.0中要如何实现呢?下面举例子说明,首先在HTML页面中设计好如下代码:

    <asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting">
    <Columns>
    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
    <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
    <asp:TemplateField HeaderText="Select">
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("CategoryID") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    在上面的代码中,我们设置了一个链接linkbutton,其中指定了commandname为"Delete",commandargument为 要删除的记录的ID编号,注意一旦commandname设置为delete这个名称后,gridview中的GridView_RowCommand 和 GridView_Row_Deleting 事件都会被激发接者,我们处理其rowdatabound事件中:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
    l.Attributes.Add('onclick", "javascript:return " + "confirm("是否要删除该记录? " +
    DataBinder.Eval(e.Row.DataItem, "id") + "')");
    }
    }
    在这段代码中,首先检查是否是datarow,是的话则得到每个linkbutton,再为其添加客户端代码,基本和asp.net 1.1的做法差不多。

    之后,当用户选择了确认删除后,我们有两种方法对其进行继续的后续删除处理,因为我们将删除按钮设置为Delete,方法一是在row_command事件中写入如下代码:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName == "Delete")
    {
    int id = Convert.ToInt32(e.CommandArgument);
    // 删除记录的专门过程
    DeleteRecordByID(id);
    }
    }
    另外一种方法是使用gridview的row_deletting事件,先在页面HTML代码中,添加<asp:GridView DataKeyNames="CategoryID" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" onRowDeleting="GridView1_RowDeleting">
    然后添加row_deleting事件:

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    int categoryID = (int) GridView1.DataKeys[e.RowIndex].Value;
    DeleteRecordByID(categoryID);
    }
    要注意的是,这个必须将datakeynames设置为要删除记录的编号,这里是categoryid.
  • 相关阅读:
    CentOS查看系统信息和资源使用已经升级系统的命令
    192M内存的VPS,安装Centos 6 minimal x86,无法安装node.js
    Linux限制资源使用的方法
    多域名绑定同一IP地址,Node.js来实现
    iOS 百度地图大头针使用
    iOS 从app跳转到Safari、从app打开电话呼叫
    设置cell背景色半透明
    AsyncSocket 使用
    iOS 监听键盘变化
    iOS 7 标签栏控制器进行模态视图跳转后变成透明
  • 原文地址:https://www.cnblogs.com/wangxiaohuo/p/570383.html
Copyright © 2011-2022 走看看