[ASP. NET] gridview custom editing, update, cancel, delete

Source: Internet
Author: User

1. Problem Description

On the Internet today, someone asked me how to customize the edit, update, cancel, and delete buttons of the gridview. The buttons in the gridview are too ugly.

2. meet the requirements

Change edit, update, cancel, delete


3. Implementation Method

(1) Step 1: Change "commandfield" of the gridview to "templatefiled template"

(2) Step 2: edit this field in "Edit template". The method is the same as that for editing other templates. In my example, add two imagebutton fields in "itemtemplate, add two linkbuttons to "edittemplate"

Edit, delete, update, and cancel

(3) Step 3: Add rowcancelingedit, rowediting, rowupdating, and rowdeleting events. I believe everyone will.

NOTE: For the four controls in step 2, you must add commandname, which are "edit", "delete", "Update", and "cancel ".


My code:

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "replace edit with a button. aspx. CS "inherits =" NOP. gridview summary. change the edit button to "%> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 


 string strCon = "Data Source=;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();        }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.