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 ".
OK!
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=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(); }