利用GridView控件实现数据添加

*在Asp.Net web开发过程中,前台的代码涉及到页面的设计,后台结合C#语言实现控件的功能。

第一步:

前台的设计的视图

利用GridView控件实现数据添加_第1张图片

第二步:

后台的设计代码

1)点击添加按钮,响应按钮的点击事件,执行JS代码

2)2.1创建web窗体,填写信息的添加

利用GridView控件实现数据添加_第2张图片

2.2点击新用户注册按钮,响应按钮的点击事件,执行C#代码

SqlConnection con = new SqlConnection(sqlstr);
                    SqlCommand cmd = con.CreateCommand();
                    con.Open();
                    cmd.CommandText = "insert into T_User(UserName,Password,Name,Address,Mobile,Birthday,Gender,Email,interest,ImgUrl,RegisterTime) values(@username,@password,@name,@address,@mobile,@birthday,@gender,@email,@interest,@imgurl,GETDATE())";

                    cmd.Parameters.AddWithValue("@username", txbUserName.Text.Trim());
                    cmd.Parameters.AddWithValue("@password", txbPwd.Text.Trim());
                    cmd.Parameters.AddWithValue("@name", txbName.Text.Trim());
                    cmd.Parameters.AddWithValue("@address", txbAddress.Text.Trim());
                    cmd.Parameters.AddWithValue("@mobile", txbMobile.Text.Trim());

                    //拼接生日日期

                    cmd.Parameters.AddWithValue("@birthday", txbBirthday.Text.Trim());

                    //判断用户选择的性别,使用RadioButtonList
                    string gender = this.rbGender.SelectedItem.Text;
                    cmd.Parameters.AddWithValue("@gender", gender);
                    //判断兴趣,使用CheckBoxList
                    string interest = "";
                    for (int i = 0; i < ckInterest.Items.Count; i++)
                    {
                        if (ckInterest.Items[i].Selected)
                        {
                            interest += ckInterest.Items[i].Text + ",";
                        }
                    }

                    interest = interest.Trim(',');
                    cmd.Parameters.AddWithValue("@interest", interest);

                    cmd.Parameters.AddWithValue("@email", txbEmail.Text.Trim());
                    cmd.Parameters.AddWithValue("@imgurl", ViewState["imgurl"]);

                    int result = cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    con.Close();
                    con.Dispose();
                    if (result > 0)
                    {
                        Session["username"] = this.txbUserName.Text.Trim();
                        Response.Redirect("WebForm1.aspx");
                    }
                    else
                    {
                        lbinfo.Text = "注册失败";
                    }

 

 

 

 

 

你可能感兴趣的:(web开发)