标签:
Response对象:响应请求
Response.Write("<script>alert(‘添加成功!‘)</script>");
Response.Redirect("Default.aspx");
Request对象:获取请求
Request["key"]来获取传递过来的值
QueryString:地址栏数据传递 ?key=value&key=value
注意事项:不需要保密的东西可以传
不要传过长东西,因为长度有限,过长会造成数据丢失
修改设置成超链接,点击打开Default6页面,把UserName的值传过去,
对数据表的增删改:
Default.aspx中添加用户
<input id="btn1" type="button" value="添加用户" /> <script> document.getElementById("btn1").onclick = function () { window.open("Default3.aspx", "_self"); }; </script>
首先数据访问类造一个添加方法
public bool Insert(Users u) {//添加 bool isok = false; cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f)"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", u.UserName); cmd.Parameters.Add("@b", u.PassWord); cmd.Parameters.Add("@c", u.NickName); cmd.Parameters.Add("@d", u.Sex); cmd.Parameters.Add("@e", u.Birthday); cmd.Parameters.Add("@f", u.Nation); conn.Open(); try { cmd.ExecuteNonQuery(); isok = true; } catch { } conn.Close(); return isok; }
添加:
<body> <form id="form1" runat="server"> <h1>用户添加</h1> 用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br /> 密码:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><br /><br /> 确认密码:<asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox><br /><br /> 昵称:<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br /><br /> 性别:<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Value="true" Selected="True">男</asp:ListItem> <asp:ListItem Value="false">女</asp:ListItem> </asp:RadioButtonList><br /><br /> 生日:<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>年<asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>月<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList>日<br /><br /> 民族:<asp:DropDownList ID="DropDownList1" runat="server" Width="122px"></asp:DropDownList><br /><br />         <asp:Button ID="Button1" runat="server" Text="注 册" /><br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </form> </body>
密码JS验证
<script type="text/javascript"> window.onload = function () {/*document操作取出密码框里内容*/ document.getElementById("Button1").onclick = function () { var pwd1 = document.getElementById("TextBox3").value; var pwd2 = document.getElementById("TextBox4").value; /* alert(pwd1);检测一下*/ /* alert(pwd2);*/ if (pwd1 != pwd2) { document.getElementById("Label2").innerText = "两次密码输入不一致"; return false;/*密码不一阻止刷新,一样就刷新*/ } }; }; </script> <style type="text/css"> #Label2 { color:red;/*Label2里所呈现的文字显示红色*/ } </style> </head>
性别默认选中,生日需三个DropDownList
<asp:ListItem Value="true" Selected="True">男</asp:ListItem>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)//数据绑定 { for (int i = DateTime.Now.Year; i >= 1900; i--) { //添加年 ListItem li = new ListItem(i.ToString(),i.ToString()); DropDownList2.Items.Add(li); } for (int i = 1; i <= 12; i++) { //月 ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList3.Items.Add(li); } for (int i = 1; i <= 31; i++) { //日 ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList4.Items.Add(li); } //取出民族的数据 DropDownList1.DataSource = new NationDA().Select(); DropDownList1.DataTextField = "NationName"; DropDownList1.DataValueField = "NationCode"; DropDownList1.DataBind(); } Button1.Click += Button1_Click;//事件委托 }
void Button1_Click(object sender, EventArgs e)
{
//1、构建一个Users对象
Users u = new Users();
u.UserName = TextBox1.Text;
u.PassWord = TextBox3.Text;
u.NickName = TextBox4.Text;
u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value);
string date = DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue;
u.Birthday = Convert.ToDateTime(date);
u.Nation = DropDownList4.SelectedItem.Value;
//2、将此对象添加到数据库去
bool ok = new UsersData().Insert(u);
//3、提示添加成功
if (ok)
{
Response.Write("<script>alert(‘添加成功!‘)</script>");
Response.Redirect("Default.aspx");
}
else
{
Response.Write("<script>alert(‘添加失败!‘)</script>");
}
//4、关闭此页面,刷新展示页面
}
}
二、删除
操作,在default.aspx主页数据显示中添加一列,点删除,打开新的网页delete.aspx执行代码后关闭,刷新主页面
<td>操作</td> <td><a href="delete.aspx"?un=<%#Eval("UserName")
>删除</a></td>
//新网页中执行的删除代码 //1、获取要删除的主键值,username,做删除的方法 string Uname = Request["un"].ToString(); 获取请求 //2、删除 new UsersDA().Delete(Uname); //3、调回Main页面 Response.Redirect("Main.aspx");
三、修改
新建窗体xiugai.aspx 数据展示页面添加一列修改,点击进入xiugai.aspx
<td><a href="#">修改</a></td>
数据操作类添加方法:
public bool Update(Users u) { bool isok = false; cmd.CommandText = "update Users set PassWord=@b,NickName=@c,Sex=@d,Birthday=@e,Nation=@f where UserName=@a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", u.UserName); cmd.Parameters.Add("@b", u.PassWord); cmd.Parameters.Add("@c", u.NickName); cmd.Parameters.Add("@d", u.Sex); cmd.Parameters.Add("@e", u.Birthday); cmd.Parameters.Add("@f", u.Nation); conn.Open(); try { cmd.ExecuteNonQuery(); isok = true; } catch { } conn.Close(); return isok; }
//1步、构建一个Users对象 Users u = new Users(); u.UserName = Label1.Text; if (TextBox3.Text == "" && TextBox4.Text == "") {//判断密码的 u.PassWord = pwd; } else { u.PassWord = TextBox3.Text; } u.NickName = TextBox6.Text; u.Sex = Convert.ToBoolean(RadioButtonList1.SelectedItem.Value); string data = DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue + "-" + DropDownList4.SelectedValue; u.Birthday = Convert.ToDateTime(data); u.Nation = DropDownList1.SelectedItem.Value; //2步、将此对象添加到数据库去,先在UserDA里修改方法 bool ok = new UsersDA().Update(u); //3步、提示修改成功 if (ok) { Response.Write("<script>alert(‘修改成功!‘)</script>"); 4步、Response.Write("<script>this.opener.location.href=‘Main.aspx‘;this.close();</script>"); //Response.Redirect("Main.aspx");//重定项 } else { Response.Write("<script>alert(‘修改失败!‘)</script>"); } //4、关闭此页面,刷新展示页面 //用JS写 } }
Response、Request、QueryString,修改,Cookies
标签:
原文地址:http://www.cnblogs.com/weiwenxin01/p/5904500.html