• GridView基本的操作 编辑 更新 取消(一)


    1.html代码
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        
    <title>GridView基本的操作 编辑 更新 取消</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
            
    <div>
                
    <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
                    
    <Columns>
                        
    <asp:BoundField DataField="au_id" HeaderText="au_id" />
                        
    <asp:BoundField DataField="au_lname" HeaderText="au_lname" />
                        
    <asp:BoundField DataField="au_fname" HeaderText="au_fname" />
                        
    <asp:BoundField DataField="phone" HeaderText="phone" />
                        
    <asp:BoundField DataField="address" HeaderText="address" />
                        
    <asp:CommandField ShowEditButton="True" HeaderText="编辑" />
                        
    <asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
                    
    </Columns>
                
    </asp:GridView>
            
    </div>
        
    </form>
    </body>
    </html>
    2.cs代码
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class GridView_GridView_DeleteUpdate1 : System.Web.UI.Page
    {
        Practice.DAL.authors authorsbll 
    = new Practice.DAL.authors();

        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    if (!IsPostBack)
            
    {
                DataBindGridView();
            }

            GridView1.RowDeleting 
    += new GridViewDeleteEventHandler(GridView1_RowDeleting);
            GridView1.RowEditing 
    += new GridViewEditEventHandler(GridView1_RowEditing);
            GridView1.RowCancelingEdit 
    += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
            GridView1.RowUpdating 
    += new GridViewUpdateEventHandler(GridView1_RowUpdating);
        }


        
    /// <summary>
        
    /// 功    能:GridView删除事件
        
    /// 作  者:PUKE
        
    /// 完成时间:2007-05-18
        
    /// 版  权:pukesys@tom.com
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>

        void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        
    {
            
    string id = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();

            authorsbll.Delete(id);
    //删除,函数自己写

            DataBindGridView();
    //重新绑定数据源
        }


        
    /// <summary>
        
    /// 功    能:GridView编辑事件
        
    /// 作  者:PUKE
        
    /// 完成时间:2007-05-18
        
    /// 版  权:pukesys@tom.com
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>

        void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        
    {
            GridView1.EditIndex 
    = e.NewEditIndex;
            DataBindGridView();
        }


        
    /// <summary>
        
    /// 功    能:GridView取消事件
        
    /// 作  者:PUKE
        
    /// 完成时间:2007-05-18
        
    /// 版  权:pukesys@tom.com
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>

        void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        
    {
            GridView1.EditIndex 
    = -1;
            DataBindGridView();
        }


        
    /// <summary>
        
    /// 功    能:GridView更新事件
        
    /// 作  者:PUKE
        
    /// 完成时间:2007-05-18
        
    /// 版  权:pukesys@tom.com
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>

        void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        
    {
            
    string aa = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
            GridView1.EditIndex 
    = -1;
            DataBindGridView();
        }
        

        
    /// <summary>
        
    /// 功    能:绑定GridView
        
    /// 作  者:PUKE
        
    /// 完成时间:2007-05-18
        
    /// 版  权:pukesys@tom.com
        
    /// </summary>

        private void DataBindGridView()
        
    {
            DataSet ds 
    = authorsbll.GetList("");
            GridView1.DataSource 
    = ds;
            GridView1.DataBind();
        }

    }

  • 相关阅读:
    C#实现MD5加密,winform c#2005
    关于 "基础连接已经关闭:接收时发生意外错误"
    SERVERPROPERTY方法说明
    Web 设计与开发终极资源大全(上)
    SQL Server:在 SQL Server 2005 中配置数据库邮件,发送邮件
    Web 地理定位(GeoLocation)知识大全
    sql server2005 创建作业问题
    SQL Server 监视事件
    Remoting 如何穿越防火墙
    使用SQL SERVER 2000的全文检索功能
  • 原文地址:https://www.cnblogs.com/puke/p/768502.html
Copyright © 2020-2023  润新知