ASP.NET(C#) GridView (编辑、删除、更新、取消)
2024-07-02 15:01:30 221人阅读
转自:http://my.oschina.net/dldlhrmc/blog/93458
前台代码
01 | <%@ Page Language= "C#" AutoEventWireup= "true" CodeFile= "Default5.aspx.cs" Inherits = "Default5" %> |
03 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > |
05 | <html xmlns= "http://www.w3.org/1999/xhtml" > |
10 | <form id= "form1" runat= "server" > |
13 | <asp:GridView ID= "GridView1" runat= "server" AutoGenerateDeleteButton= "True" |
14 | AutoGenerateEditButton= "True" DataKeyNames= "id" |
15 | onrowdeleting= "GridView1_RowDeleting" onrowediting= "GridView1_RowEditing" |
16 | onrowupdating= "GridView1_RowUpdating" |
17 | onrowcancelingedit= "GridView1_RowCancelingEdit" > |
后台代码
02 | using System.Collections.Generic; |
06 | using System.Web.UI.WebControls; |
07 | using System.Data.SqlClient; |
10 | public partial class Default5 : System.Web.UI.Page |
12 | private void BindData() |
14 | String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True" ; |
15 | SqlConnection con = new SqlConnection(Strcon); |
16 | String sql = "select userinfo.id,username,password,birthday from userinfo" ; |
17 | SqlDataAdapter ad = new SqlDataAdapter(sql, con); |
18 | DataSet ds = new DataSet(); |
20 | GridView1.DataSource = ds; |
24 | protected void Page_Load(object sender, EventArgs e) |
26 | if (!IsPostBack) /*如果省略这句,下面的更新操作将无法完成,因为获得的值是不变的*/ |
31 | protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) |
33 | String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True" ; |
34 | SqlConnection con = new SqlConnection(Strcon); |
35 | int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置 DataKeyNames,这里设为 id */ |
36 | String sql = "delete from userinfo where id=‘" + id+ "‘" ; |
38 | SqlCommand com = new SqlCommand(sql, con); |
40 | com.ExecuteNonQuery(); |
46 | /*编辑操作,利用e.NewEditIndex获取当前编辑行索引*/ |
47 | protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) |
49 | GridView1.EditIndex = e.NewEditIndex; |
50 | BindData(); /*再次绑定显示编辑行的原数据,不进行绑定要点2次编辑才能跳到编辑状态*/ |
52 | protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) |
54 | String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True" ; |
55 | SqlConnection con = new SqlConnection(Strcon); |
56 | String username = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text.ToString(); /*获取要更新的数据*/ |
57 | String password = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text.ToString(); |
58 | String birthday = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString(); |
60 | int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置 DataKeyNames,这里设为 id */ |
62 | String sql = "update userinfo set username=‘" + username + "‘,password=‘" +password+ "‘,birthday=‘" +birthday+ "‘ where id=‘" +id+ "‘" ; |
64 | SqlCommand com = new SqlCommand(sql, con); |
66 | com.ExecuteNonQuery(); |
68 | GridView1.EditIndex = -1; |
71 | protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) |
73 | GridView1.EditIndex = -1; /*编辑索引赋值为-1,变回正常显示状态*/ |
SQL 脚本(SQL Server)
04 | /****** Object: Table [dbo].[userinfo] Script Date : 12/02/2012 22:20:46 ******/ |
08 | SET QUOTED_IDENTIFIER ON |
14 | CREATE TABLE [dbo].[userinfo]( |
15 | [id] [ int ] IDENTITY(1,1) NOT NULL , |
16 | [username] [ varchar ](50) NULL , |
17 | [ password ] [ varchar ](50) NULL , |
18 | [birthday] [ varchar ](50) NULL , |
19 | CONSTRAINT [PK_userinfo] PRIMARY KEY CLUSTERED |
22 | ) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ PRIMARY ] |
本人菜鸟,勿喷!仅供参考。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉:
投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。