通过GridView控件里面的就是Commandfield里面的编辑更新取消按钮来更新数据库求,跪求后台代码啊 求大神吗救救我啊 我刚学ASP.net啊 本人菜的狠啊

解决方案 »

  1.   

    http://www.cnblogs.com/dushouke/archive/2008/03/24/1120377.html
      

  2.   

    连本入门书都不舍得买的话,可以看这个很古老的教程。这个教程出来的时候,连asp.net ajax都还不存在,所以他不涉及那部分。http://www.cnblogs.com/Reeezak/archive/2007/08/13/853925.html
      

  3.   


    edit  (ing,ed)
    update (ing,ed)
    cancel事件的
      

  4.   

    这是我写的一个公告编辑删除的一个程序;你看看吧!希望对你有帮助!!  PS:学ASP.NET真的很痛苦啊!
    public partial class BulletinManage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridView();
                }
            }        private void BindGridView()
            {            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True");
                string sqlStr = "select * from T_Bulletin";
                SqlDataAdapter dap = new SqlDataAdapter(sqlStr, con);
                DataSet ds = new DataSet();
                //con.Open();
                dap.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataKeyNames = new string[] { "BulletinID" };
                GridView1.DataBind();
                //con.Close();
                //显示当前页数
                lblPageSum.Text = "当前页为" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + "页";        }        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                //重新获得页索引
                GridView1.PageIndex = e.NewPageIndex;
                BindGridView();
            }        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {                //设置日期格式
                    e.Row.Cells[3].Text = e.Row.Cells[3].Text != "" ? Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString() : "2011-10-24 ";                //e.Row.Cells[3].Text = Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString();                //高亮显示指定行
                    e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#f6b33e'");
                    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;");                //删除时弹出对话框
                    //((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm ('确定删除吗?')");
                              }
            }        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                //删除公告
                string sqlStr = "delete from T_Bulletin where BulletinID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True");
                SqlCommand cmd = new SqlCommand(sqlStr, con);
                con.Open();            //执行SQL语句
                cmd.ExecuteNonQuery();
                con.Close();
                BindGridView();
            }        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                //新的编辑列索引指定给当前索引
                GridView1.EditIndex = e.NewEditIndex;
                BindGridView();
            }        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                //把编辑索引设为-1
                GridView1.EditIndex = -1;
                BindGridView();
            }        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                //获取要更新字段的ID值
                string BulletinID = GridView1.DataKeys[e.RowIndex].Value.ToString();
                string Title = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
                string Content = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;            //编写修改的SQL语句
                string strSql = "update T_Bulletin set Title='"+Title +"',Content='"+Content +"'where BulletinID='"+BulletinID +"'";            //执行修改操作
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True");            //初始化执行命令
                SqlCommand cmd = new SqlCommand(strSql ,con );            con.Open();
                cmd.ExecuteNonQuery();
                con.Close();            //取消编辑状态
                GridView1.EditIndex = -1;            //重新绑定
                BindGridView();        }       
        }