[ASP.NET]GridView自訂編輯,更新,取消,刪除

來源:互聯網
上載者:User

 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大功告成!

My Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="將編輯換成按鈕.aspx.cs" Inherits="NOP.GridView總匯.將編輯換成按鈕" %><!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"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CID"            OnRowCancelingEdit="GridView1_RowCancelingEdit"             OnRowUpdating="GridView1_RowUpdating"            OnRowEditing="GridView1_RowEditing"             OnRowDeleting="GridView1_RowDeleting">            <Columns>                <asp:TemplateField HeaderText="編號">                    <ItemTemplate>                        <asp:Label Text='<%# Eval("CID") %>' runat="server"></asp:Label>                    </ItemTemplate>                    <EditItemTemplate>                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CID") %>' ></asp:TextBox>                    </EditItemTemplate>                </asp:TemplateField>                <asp:TemplateField HeaderText="班級名">                    <ItemTemplate>                        <%# DataBinder.Eval(Container.DataItem,"CName") %>                    </ItemTemplate>                    <EditItemTemplate>                        <asp:TextBox ID="TextBox2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"CName") %>'></asp:TextBox>                    </EditItemTemplate>                </asp:TemplateField>                <asp:TemplateField>                    <ItemTemplate>                        <asp:ImageButton ID="Button2" runat="server" ImageUrl="~/Imgs/btnEdit.png" CommandName="Edit" />                      <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Imgs/btnDelete.png" CommandName="Delete" />                    </ItemTemplate>                    <EditItemTemplate>                        <asp:LinkButton ID="Button1" runat="server" Text="更新" CommandName="Update" />                        <asp:LinkButton ID="Button3" runat="server" Text="取消" CommandName="Cancel" />                    </EditItemTemplate>                </asp:TemplateField>            </Columns>        </asp:GridView>    </div>    </form></body></html>

 

 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();        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.