• ASP.NET


    加载:

    点击编辑:

    数据库设计:

    前端代码:

    1.  DataKeyNames="ID"  设置点击“编辑”选项的时候,要获取的值,一般获取ID主键,便于修改数据。
    2.  AutoGenerateColumns="False"  设置“设置是否自动生成列”为False。
    3.  ReadOnly="True"  设置为只读,这样在点击编辑的时候,设置为只读的字段,不会变成文本框。
    4.  DataField="ID"  设置绑定数据的字段名称。
    5.  OnRowEditing ="GridView1_RowEditing"  编辑。
    6.  OnRowDeleting ="GridView1_RowDeleting"  删除。
    7.  OnRowCancelingEdit ="GridView1_RowCancelingEdit"  取消。
    8.  OnRowUpdating ="GridView1_RowUpdating1">  更新。
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"
            DataKeyNames="ID"
            AutoGenerateColumns="False"
            OnRowEditing ="GridView1_RowEditing"
            OnRowDeleting ="GridView1_RowDeleting"
            OnRowCancelingEdit ="GridView1_RowCancelingEdit"
            OnRowUpdating ="GridView1_RowUpdating1">
            <Columns>
                <asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="True" />
                <asp:BoundField HeaderText="Name" DataField="Name" ReadOnly="True" />
                <asp:BoundField HeaderText="Score" DataField="Score"/>
                <asp:CommandField HeaderText ="删除" ShowDeleteButton="true" />
                <asp:CommandField HeaderText="修改" ShowEditButton="True" />
            </Columns>
        </asp:GridView>
    </div>
    </form>

    后台代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindSoure();
        }
    }
    
    /// <summary>
    /// 绑定数据源
    /// </summary>
    public void BindSoure()
    {
        this.GridView1.DataSource = SQLHelper.ExecuteTable("select ID, Name, Score from Tb_Mark", System.Data.CommandType.Text);
        this.GridView1.DataBind();
    }
    
    /// <summary>
    /// 编辑记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //获得编辑列
        GridView1.EditIndex = e.NewEditIndex;
        //重新绑定数据
        BindSoure();
    }
    
    /// <summary>
    /// 删除记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取编辑列的键值对中的值。在属性的DataKeyNames中设置。对一个的ID
        int UpdateIdentifier = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
    
        string text = "delete from Tb_Mark where ID = '" + UpdateIdentifier + "'";
    
        if (SQLHelper.ExecuteNonQuery(text, System.Data.CommandType.Text) != 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('删除失败!')</script>");
        }
        //重新绑定数据
        BindSoure();
    }
    
    /// <summary>
    /// 取消修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //退出编辑状态
        GridView1.EditIndex = -1;
        //重新绑定数据
        BindSoure();
    }
    
    /// <summary>
    /// 更新修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
        //获取编辑列的键值对中的值。在属性的DataKeyNames中设置。对一个的ID
        int UpdateIdentifier = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        //获取第三列Score的值(从0开始),要修改的值。
        int UpdateValue = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString());
    
        string text = "update Tb_Mark set Score = '" + UpdateValue + "' where ID = '" + UpdateIdentifier + "'";
    
        if (SQLHelper.ExecuteNonQuery(text, System.Data.CommandType.Text) != 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('更新失败!')</script>");
        }
        //退出编辑状态
        GridView1.EditIndex = -1;
        //重新绑定数据
        BindSoure();
    }
    

      

      

      

  • 相关阅读:
    python3.x元组打印错误 TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
    LoRa---她的简介和她的专业术语
    单片机程序在内存和FLASH中的空间分配
    STM32烧录的常用方式
    【原创】MapReduce编程系列之表连接
    Maven基础配置—本地Maven配置
    Maven基础配置--nexus私服配置
    OSGI入门笔记
    Maven仓库搭建--nexus私服
    javascript基础 方法 函数 闭包 集合
  • 原文地址:https://www.cnblogs.com/KTblog/p/4752461.html
Copyright © 2020-2023  润新知