页面源代码
1 <div>
2 <a href="test.aspx?id=add" style="text-decoration:none">增加一个用户</a>
3 <asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
4 <Columns>
5 <asp:BoundField DataField ="CustomerID" HeaderText ="姓名" />
6 <asp:BoundField DataField ="CompanyName" HeaderText ="公司名称" />
7 <asp:BoundField DataField ="ContactName" HeaderText ="联系人" />
8 <asp:BoundField DataField ="ContactTitle" HeaderText ="职务" />
9 <asp:TemplateField HeaderText ="修改">
10 <ItemTemplate>
11 <a href="test.aspx?id=edit&userid=<%#Eval("CustomerID") %>"><%#Eval("City") %></a>
12 </ItemTemplate>
13 </asp:TemplateField>
14 </Columns>
15 </asp:GridView>
16 </div>
后台代码:
增加用户与修改用户信息的后台代码
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (Request.QueryString["id"] == null)
4 {
5 Page.RegisterStartupScript("", "<script language=javascript>alert('登陆错误!');location='Default.aspx'</script>");
6 }
7 else
8 {
9 if (Request.QueryString["id"] == "edit")
10 {
11
12 if (!IsPostBack)
13 {
14 databind();
15 }
16 }
17 else
18 {
19 this.TextBox1.ReadOnly = false;
20 if (!IsPostBack)
21 {
22 this.TextBox2.Enabled = false;
23 this.TextBox3.Enabled = false;
24 this.TextBox4.Enabled = false;
25
26 }
27 }
28 }
29
30 }
31 public void databind()
32 {
33 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
34 SqlCommand cmd = new SqlCommand("Select * From Customers where CustomerID =@CustomerID", con);
35 SqlParameter sp = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
36 sp.Value = Request.QueryString["userid"].ToString();
37 cmd.Parameters.Add(sp);
38 SqlDataAdapter da = new SqlDataAdapter(cmd);
39 DataSet ds = new DataSet();
40 da.Fill(ds);
41 this.TextBox1.Text = Request.QueryString["userid"];
42 this.TextBox2.Text = ds.Tables[0].Rows[0]["CompanyName"].ToString();
43 this.TextBox3.Text = ds.Tables[0].Rows[0]["ContactName"].ToString();
44 this.TextBox4.Text = ds.Tables[0].Rows[0]["ContactTitle"].ToString();
45 }
46 protected void Button1_Click(object sender, EventArgs e)
47 {
48 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
49 if (Request.QueryString["id"] == "edit")
50 {
51 SqlCommand cmd = new SqlCommand("Update Customers set CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle where CustomerID =@CustomerID", con);
52 SqlParameter[] sp = new SqlParameter[4];
53
54 sp[0] = new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40);
55 sp[1] = new SqlParameter("@ContactName", SqlDbType.NVarChar, 30);
56 sp[2] = new SqlParameter("@ContactTitle", SqlDbType.NVarChar, 30);
57 sp[3] = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
58 sp[0].Value = this.TextBox2.Text.Trim();
59 sp[1].Value = this.TextBox3.Text.Trim();
60 sp[2].Value = this.TextBox4.Text.Trim();
61 sp[3].Value = this.TextBox1.Text.Trim();
62 cmd.Parameters.AddRange(sp);
63 if (con.State == ConnectionState.Closed)
64 {
65 con.Open();
66 }
67 cmd.ExecuteNonQuery();
68 con.Close();
69 Page.RegisterStartupScript("", "<script language=javascript>alert('修改成功!');location='Default.aspx'</script>");
70 }
71 else if (Request.QueryString["id"] == "add")
72 {
73
74 SqlCommand cmd = new SqlCommand();
75 cmd.Connection = con;
76 cmd.CommandText = "Insert into Customers(CustomerID,CompanyName,ContactName,ContactTitle) values(@CustomerID,@CompanyName,@ContactName,@ContactTitle)";
77 SqlParameter[] sp = new SqlParameter[4];
78 sp[0] = new SqlParameter("@CustomerID",SqlDbType.NChar,5);
79 sp[1] = new SqlParameter("@CompanyName",SqlDbType.NVarChar,40);
80 sp[2] = new SqlParameter("@ContactName",SqlDbType.NVarChar,30);
81 sp[3] = new SqlParameter("@ContactTitle",SqlDbType.NVarChar,30);
82 sp[0].Value = this.TextBox1.Text.Trim();
83 sp[1].Value = this.TextBox2.Text.Trim();
84 sp[2].Value = this.TextBox3.Text.Trim();
85 sp[3].Value = this.TextBox4.Text.Trim();
86 cmd.Parameters.AddRange(sp);
87 if (con.State == ConnectionState.Closed)
88 {
89 con.Open();
90 }
91 cmd.ExecuteNonQuery();
92 con.Close();
93 Page.RegisterStartupScript("", "<script language=javascript>alert('修改成功!');location='Default.aspx'</script>");
94 }
95 }
96 protected void TextBox1_TextChanged(object sender, EventArgs e)
97 {
98 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
99 SqlCommand cmd = new SqlCommand("Select * From Customers where CustomerID =@CustomerID", con);
100 SqlParameter sp = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
101 sp.Value = this.TextBox1.Text.Trim();
102 cmd.Parameters.Add(sp);
103 SqlDataAdapter da = new SqlDataAdapter(cmd);
104 DataSet ds = new DataSet();
105 da.Fill(ds);
106 if (ds.Tables[0].Rows.Count > 0)
107 {
108 this.Label5.Text = "用户名已经存在!";
109 this.TextBox2.Enabled = false;
110 this.TextBox3.Enabled = false;
111 this.TextBox4.Enabled = false;
112 }
113 else
114 {
115 this.Label5.Text = "用户名不存在!";
116 this.TextBox2.Enabled = true;
117 this.TextBox3.Enabled = true;
118 this.TextBox4.Enabled = true;
119 }
120 }
121
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏