您的位置:首页 > 编程语言 > ASP

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

2011-12-13 10:48 756 查看
 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();

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息