Gridview中RowCommand事件的应用

一直不喜欢用Gridview,可是任务说明要用Gridview的RowCommand事件,

把代码整理如下:

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e)
2 {
3 if (e.CommandName == " Up " )
4 {
5 string [] skey = e.CommandArgument.ToString().Split( ' , ' );
6
7 SqlConnection con = new SqlConnection( @" Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True " );
8 SqlCommand cmd = new SqlCommand();
9 cmd.Connection = con;
10 cmd.CommandText = " update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle where id =@id " ;
11 SqlParameter[] sp = new SqlParameter[ 4 ];
12 sp[ 0 ] = new SqlParameter( " @CompanyName " , SqlDbType.NVarChar, 40 );
13 sp[ 1 ] = new SqlParameter( " @ContactName " , SqlDbType.NVarChar, 30 );
14 sp[ 2 ] = new SqlParameter( " @ContactTitle " , SqlDbType.NVarChar, 30 );
15 sp[ 3 ] = new SqlParameter( " @id " , SqlDbType.UniqueIdentifier);
16 sp[ 0 ].Value = ((TextBox) this .GridView1.Rows[ int .Parse(skey[ 0 ])].FindControl( " TextBox2 " )).Text.Trim();
17 sp[ 1 ].Value = ((TextBox) this .GridView1.Rows[ int .Parse(skey[ 0 ])].FindControl( " TextBox3 " )).Text.Trim();
18 sp[ 2 ].Value = ((TextBox) this .GridView1.Rows[ int .Parse(skey[ 0 ])].FindControl( " TextBox4 " )).Text.Trim();
19 sp[ 3 ].Value = new Guid(skey[ 1 ]);
20 cmd.Parameters.AddRange(sp);
21 if (con.State == ConnectionState.Closed)
22 {
23 con.Open();
24 }
25 int i = cmd.ExecuteNonQuery();
26 if (i == 1 )
27 {
28 Page.RegisterStartupScript( "" , " <script language=javascript>alert('修改成功!');location='default2.aspx'</script> " );
29 }
30 else
31 {
32 Page.RegisterStartupScript( "" , " <script language=javascript>alert('修改失败!');location='default2.aspx'</script> " );
33 }
34 this .GridView1.EditIndex = - 1 ;
35
36 }
37 if (e.CommandName == " Ca " )
38 {
39
40 this .GridView1.EditIndex = - 1 ;
41
42 }
43 if (e.CommandName == " Ed " )
44 {
45 this .GridView1.EditIndex = int .Parse(e.CommandArgument.ToString());
46 }
47 bind();
48 }

前台:

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 < asp:GridView ID = " GridView1 " runat = " server " AutoGenerateColumns = " False " BackColor = " White "
2 BorderColor = " #CCCCCC " BorderStyle = " None " BorderWidth = " 1px " CellPadding = " 3 " OnRowCommand = " GridView1_RowCommand " >
3 < Columns >
4 < asp:TemplateField HeaderText = " 姓名 " >
5 < ItemTemplate >
6 <% #Eval( " CustomerID " ) %>
7 </ ItemTemplate >
8 < EditItemTemplate >
9 <% #Eval( " CustomerID " ) %>
10 </ EditItemTemplate >
11 </ asp:TemplateField >
12 < asp:TemplateField HeaderText = " 公司 " >
13 < ItemTemplate >
14 <% #Eval( " CompanyName " ) %>
15 </ ItemTemplate >
16 < EditItemTemplate >
17 < asp:TextBox ID = " TextBox2 " runat = " server " Text = ' <%#Eval("CompanyName") %> ' ></ asp:TextBox >
18 </ EditItemTemplate >
19 </ asp:TemplateField >
20 < asp:TemplateField HeaderText = " 联系人 " >
21 < ItemTemplate >
22 <% #Eval( " ContactName " ) %>
23 </ ItemTemplate >
24 < EditItemTemplate >
25 < asp:TextBox ID = " TextBox3 " runat = " server " Text = ' <%#Eval("ContactName") %> ' ></ asp:TextBox >
26 </ EditItemTemplate >
27 </ asp:TemplateField >
28 < asp:TemplateField HeaderText = " 职务 " >
29 < ItemTemplate >
30 <% #Eval( " ContactTitle " ) %>
31 </ ItemTemplate >
32 < EditItemTemplate >
33 < asp:TextBox ID = " TextBox4 " runat = " server " Text = ' <%#Eval("ContactTitle") %> ' ></ asp:TextBox >
34 </ EditItemTemplate >
35 </ asp:TemplateField >
36 < asp:TemplateField HeaderText = " 操作 " >
37 < ItemTemplate >
38 < asp:LinkButton ID = " LinkButton1 " runat = " server " CommandName = " Ed " CommandArgument = ' <%#GridView1.Rows.Count.ToString() %> ' > 修改 </ asp:LinkButton >
39 </ ItemTemplate >
40 < EditItemTemplate >
41 < asp:LinkButton ID = " LinkButton2 " runat = " server " CommandName = " Up " CommandArgument = ' <%#GridView1.Rows.Count.ToString()+","+Eval("id") %> ' > 修改 </ asp:LinkButton >
42 < asp:LinkButton ID = " LinkButton3 " runat = " server " CommandName = " Ca " CommandArgument = ' <%#GridView1.Rows.Count.ToString() %> ' > 取消 </ asp:LinkButton >
43 </ EditItemTemplate >
44 </ asp:TemplateField >
45 </ Columns >
46 < FooterStyle BackColor = " White " ForeColor = " #000066 " />
47 < RowStyle ForeColor = " #000066 " />
48 < PagerStyle BackColor = " White " ForeColor = " #000066 " HorizontalAlign = " Left " />
49 < SelectedRowStyle BackColor = " #669999 " Font - Bold = " True " ForeColor = " White " />
50 < HeaderStyle BackColor = " #006699 " Font - Bold = " True " ForeColor = " White " />
51 </ asp:GridView >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值