[ASP.NET]GridView自定义编辑,更新,取消,删除

 1,问题描述

今天在网上看见有人问怎么样自定义GridView的编辑,更新,取消,删除按钮,GridView自带的太难看

2,达到要求

变换编辑,更新,取消,删除按钮

 

3,实现方法

 (1)第一步,将GridView的“CommandField”变为“TemplateFiled模板”

 (2)第二步,将在“编辑模板”里编辑此字段,方法和编辑其他模板一样,比如我的例子是在“ItemTemplate”加入两个ImageButton,在“EditTemplate”中加入两个LinkButton

分别代表编辑,删除,更新,取消

 (3)第三步,就是添加 RowCancelingEdit,RowEditing,RowUpdating,RowDeleting 这些事件,相信大家都会的。

注意:在第二步中的四个控件,需要加上 CommandName,分别为“Edit”,“Delete”,“Update”,“Cancel”。

Ok大功告成!

我的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="将编辑换成按钮.aspx.cs" Inherits="NOP.GridView总汇.将编辑换成按钮" %>





    


    
<%# DataBinder.Eval(Container.DataItem,"CName") %>


 

 string strCon = "Data Source=192.168.0.105;Initial Catalog=TreeView;Integrated Security=True";
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            SqlConnection con = new SqlConnection(strCon);

            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT [CID], [CName] FROM [ClassTable]", con);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            this.GridView1.DataSource = ds.Tables[0];
            this.GridView1.DataBind();
            con.Close();
        }


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

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

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            string CID = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
            string CName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
            string strUpdate = "UPDATE ClassTable set CID='" + CID + "',CName='" + CName + "' where CID=" + CID;

            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand(strUpdate, con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridView1.EditIndex = -1;
            BindGridView();

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            string strDel = "DELETE FROM ClassTable where CID=" + id;
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand(strDel, con);
            cmd.ExecuteNonQuery();
            con.Close();
            BindGridView();

        }


 

你可能感兴趣的:(ASP.NET)