工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

把代码整理如下:

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 }

前台:

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>
posted on 2011-05-18 15:39  梦里故乡  阅读(3802)  评论(0编辑  收藏  举报