[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总汇.将编辑换成按钮" %>

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

        }


 

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值