Gridview中的编辑模板与项模板的用法

前台代码书写:

ContractedBlock.gif ExpandedBlockStart.gif 前台代码
 
   
1 < asp:GridView ID = " GridView1 " AutoGenerateColumns = " false " runat = " server " BorderColor = " Black "
2 onrowcancelingedit = " GridView1_RowCancelingEdit "
3 onrowediting = " GridView1_RowEditing "
4 onrowdatabound = " GridView1_RowDataBound " onrowupdating = " GridView1_RowUpdating " >
5 < Columns >
6 < asp:TemplateField HeaderText = " ID " >
7 < ItemTemplate >
8 <% #Eval( " CustomerID " ) %>
9 </ ItemTemplate >
10 < EditItemTemplate >
11 <% #Eval( " CustomerID " ) %>
12 </ EditItemTemplate >
13 </ asp:TemplateField >
14 < asp:TemplateField HeaderText = " Name " >
15 < ItemTemplate >
16 <% #Eval( " CompanyName " ) %>
17 </ ItemTemplate >
18 < EditItemTemplate >
19 < asp:TextBox ID = " TextBox1 " runat = " server " Text = ' <%#Eval("CompanyName") %> ' ></ asp:TextBox >
20 </ EditItemTemplate >
21 </ asp:TemplateField >
22 < asp:TemplateField HeaderText = " Contact " >
23 < ItemTemplate >
24 <% #Eval( " ContactName " ) %>
25 </ ItemTemplate >
26 < EditItemTemplate >
27 < asp:TextBox ID = " TextBox2 " runat = " server " Text = ' <%#Eval("ContactName") %> ' ></ asp:TextBox >
28 </ EditItemTemplate >
29 </ asp:TemplateField >
30 < asp:TemplateField HeaderText = " Address " >
31 < ItemTemplate >
32 <% #Eval( " Address " ) %>
33 </ ItemTemplate >
34 < EditItemTemplate >
35 < asp:TextBox ID = " TextBox3 " runat = " server " Text = ' <%#Eval("Address") %> ' ></ asp:TextBox >
36 </ EditItemTemplate >
37 </ asp:TemplateField >
38 < asp:TemplateField HeaderText = " City " >
39 < ItemTemplate >
40 <% #Eval( " City " ) %>
41 </ ItemTemplate >
42 < EditItemTemplate >
43 < asp:DropDownList ID = " DropDownList1 " AutoPostBack = " true " runat = " server " >
44 </ asp:DropDownList >
45 </ EditItemTemplate >
46 </ asp:TemplateField >
47 < asp:TemplateField HeaderText = " 编辑 " >
48 < ItemTemplate >
49 < asp:Button ID = " Button1 " runat = " server " Text = " 编辑 " CommandName = " Edit " />
50 </ ItemTemplate >
51 < EditItemTemplate >
52 < asp:Button ID = " Button2 " runat = " server " Text = " 修改 " CommandName = " Update " />& nbsp;
53 < asp:Button ID = " Button3 " runat = " server " Text = " 取消 " CommandName = " Cancel " />
54 </ EditItemTemplate >
55 </ asp:TemplateField >
56 </ Columns >
57 </ asp:GridView >

后台代码:

ContractedBlock.gif ExpandedBlockStart.gif 后台代码
 
   
1 protected void Page_Load( object sender, EventArgs e)
2 {
3 if ( ! IsPostBack)
4 {
5 databind();
6 }
7 }
8 public void databind()
9 {
10 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ " Conn " ].ToString());
11 SqlCommand cmd = new SqlCommand( " SELECT * FROM CUSTOMERS " , con);
12 SqlDataAdapter da = new SqlDataAdapter(cmd);
13 DataSet ds = new DataSet();
14 da.Fill(ds);
15 this .GridView1.DataSource = ds.Tables[ 0 ];
16 this .GridView1.DataKeyNames = new string [] { " CustomerID " , " City " };
17 this .GridView1.DataBind();
18 }
19 protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
20 {
21 this .GridView1.EditIndex = e.NewEditIndex;
22 databind();
23 }
24 protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
25 {
26 this .GridView1.EditIndex = - 1 ;
27 databind();
28 }
29 protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e)
30 {
31 foreach (TableCell item in e.Row.Cells)
32 {
33 item.Attributes.Add( " style " , " border-color:black " );
34
35 }
36 if (e.Row.RowType == DataControlRowType.DataRow)
37 {
38 DropDownList dr = (DropDownList)e.Row.FindControl( " DropDownList1 " );
39 if (dr != null )
40 {
41 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ " Conn " ].ToString());
42 SqlCommand cmd = new SqlCommand( " Select distinct City from Customers " , con);
43 SqlDataAdapter da = new SqlDataAdapter(cmd);
44 DataSet ds = new DataSet();
45 da.Fill(ds);
46 dr.DataSource = ds.Tables[ 0 ];
47 dr.DataTextField = " City " ;
48 dr.DataValueField = " City " ;
49 dr.DataBind();
50 dr.SelectedItem.Text = this .GridView1.DataKeys[e.Row.RowIndex].Values[ 1 ].ToString();
51 }
52 }
53 }
54 protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
55 {
56 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ " Conn " ].ToString());
57 SqlCommand cmd = new SqlCommand( " Update Customers set CompanyName=@CompanyName,ContactName=@ContactName,City=@City where CustomerID=@CustomerID " , con);
58 SqlParameter[] ps = new SqlParameter[ 4 ];
59 ps[ 0 ] = new SqlParameter( " @CompanyName " ,SqlDbType.NVarChar, 40 );
60 ps[ 1 ] = new SqlParameter( " @ContactName " ,SqlDbType.NVarChar, 30 );
61 ps[ 2 ] = new SqlParameter( " @City " ,SqlDbType.NVarChar, 15 );
62 ps[ 3 ] = new SqlParameter( " @CustomerID " ,SqlDbType.NChar, 5 );
63 ps[ 0 ].Value = ((TextBox) this .GridView1.Rows[e.RowIndex].FindControl( " TextBox1 " )).Text.Trim();
64 ps[ 1 ].Value = ((TextBox) this .GridView1.Rows[e.RowIndex].FindControl( " TextBox2 " )).Text.Trim();
65 ps[ 2 ].Value = ((DropDownList) this .GridView1.Rows[e.RowIndex].FindControl( " DropDownList1 " )).SelectedItem.Text;
66 ps[ 3 ].Value = this .GridView1.DataKeys[e.RowIndex].Values[ 0 ];
67 cmd.Parameters.AddRange(ps);
68 if (con.State == ConnectionState.Closed)
69 {
70 con.Open();
71 }
72 cmd.ExecuteNonQuery();
73 this .GridView1.EditIndex = - 1 ;
74 databind();
75 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值