一、内置对象
(一)Response对象
1、简介:response 对象在cookie的值等.
2、方法:①、write方法:response.write **
功能:向客户端发送浏览器能够处理的各种数据,包括:脚本程序等.
实例:response.write "I LOVE YOU !!"
②、redirect方法:response.redirect("url")的作用是在服务器端重定向于另一个网页。
(二)Request对象
1、简介:Request对象的作用是与客户端交互,收集客户端的Form、Cookies、超链接,或者收集服务器端的环境变量。
request对象是从客户端向服务器发出请求,包括用户提交的信息以及客户端的一些信息。客户端可通过HTML表单或在网页地址后面提供参数的方法提交数据,然后通过request对象的相关方法来获取这些数据。request的各种方法主要用来处理客户端浏览器提交的请求中的各项参数和选项。
2、Request对象的五个集合:①、QueryString:用以获取客户端附在url地址后的查询字符串中的信息。
Add前台
例如:stra=Request.QueryString ["strUserld"]
前台传递写法:地址 ?key=value&key=value
注意事项:●不需要保密的东西可以传,在地址栏中是可见的,可更改的。
●不要传过长东西,因为长度有限,过长会造成数据丢失。
●不要传过长东西,因为长度有限,过长会造成数据丢失。
②、Form:用以获取客户端在FORM表单中所输入的信息。(表单的method属性值需要为POST)
例如:stra=Request.Form["strUserld"]
③、Cookies:用以获取客户端的Cookie信息。
例如:stra=Request.Cookies["strUserld"]
④、ServerVariables:用以获取客户端发出的HTTP请求信息中的头信息及服务器端环境变量信息。
例如:stra=Request.ServerVariables["REMOTE_ADDR"],返回客户端IP地址
⑤、ClientCertificate:用以获取客户端的身份验证信息
主页后台
例如:stra=Request.ClientCertificate["VALIDFORM"],对于要求安全验证的网站,返回有效起始日期。
二、利用Response对象和Request对象对Reparter中数据进行增删改
主页前台代码:
View Code
</style>
<%--光棒效果--%>
<script type="text/javascript">
window.onload = function () {
var items = document.getElementsByClassName("tr_Item");
var oldColor = "";
for (var i = 0; i < items.length; i++) {
items[i].onmouseover = function () {
oldColor = this.style.backgroundColor;
this.style.backgroundColor = "yellow";
};
items[i].onmouseout = function () {
this.style.backgroundColor = oldColor;
};
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div >
<a href ="Login.aspx"><asp:Label ID="Labdl" runat="server" Text="[请登录]"></asp:Label></a>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Button ID="Btntc" runat="server" Text="退出登陆" />
</div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table id="tb1">
<tr id="tr_head">
<td>用户名</td>
<td>密码</td>
<td>昵称</td>
<td>性别</td>
<td>生日</td>
<td>年龄</td>
<td>民族</td>
<td>操作</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="tr_Item" style="<%#Eval("Red")%>">
<td><%#Eval("UserName") %></td>
<td><%#Eval("PassWord") %></td>
<td><%#Eval("NickName") %></td>
<td><%#Eval("SexStr") %></td>
<td><%#Eval("BirthdayStr") %></td>
<td><%#Eval("Age") %></td>
<td><%#Eval("NationName") %></td>
<td> <a href="Delete.aspx?un=<%#Eval("UserName") %>" onclick="Del" >删除</a>
<a href="Update.aspx?un=<%#Eval("UserName") %>" target="_blank" onclick="Update">修改</a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="btn1" runat="server" Text="添加用户" />
<%--<input id="btn1" type="button" value="添加用户" /><br />--%>
<%--<script>
document.getElementById("btn1").onclick = function () {
window.open("Add.aspx", "_blank");
};
</script>--%>
</form>
</body>
</html>
主页后台代码:
protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Users u = new UsersDA().Select(Request.Cookies["user"].Value); Labdl.Text = u.NickName; Literal1.Text = ",欢迎你!"; } if (!IsPostBack) { Repeater1.DataSource = new UsersDA().Select(); Repeater1.DataBind(); } Btntc.Click += Btntc_Click; btn1.Click += btn1_Click; } void btn1_Click(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Add.aspx"); } else { Response.Redirect("Login.aspx"); } } void Btntc_Click(object sender, EventArgs e) { //1清除cookies Response.Cookies["user"].Expires = DateTime.Now.AddDays(-5); //2刷新页面/跳到登陆页面 Response.Redirect("Login.aspx"); } public void Del(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Delete.aspx"); } else { Response.Redirect("Login.aspx"); } } public void Update(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Update.aspx"); } else { Response.Redirect("Login.aspx"); } }
点击主页“”增加用户“按钮”,跳转到Add(添加)页面。
(一)增加
Add页面前台代码:
<title></title>
<%--判断两次密码是否一致--%>
<script type="text/javascript">
window.onload = function () {
document.getElementById("Button1").onclick = function () {
var pwd1 = document.getElementById("TextBox2").value;
var pwd2 = document.getElementById("TextBox3").value;
if (pwd1 != pwd2) {
document.getElementById("Label1").innerText = "两次密码不一致!";
return false;
}
};
};
</script>
<style type="text/css">
#Label1 {
color: red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h1>用户添加</h1>
用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
密码:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br />
<br />
重复密码:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<br />
昵称:<asp:TextBox ID="TextBox4" 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="DropDownList1" runat="server"></asp:DropDownList>年
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>月
<asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日
<br />
<br />
民族:<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList><br />
<br />
<asp:Button ID="Button1" runat="server" Text="添加" />
</form>
</body>
</html>
Add页面后台代码:
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()); DropDownList1.Items.Add(li); } for (int i = 1; i <= 12; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList2.Items.Add(li); } for (int i = 1; i <= 31; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList3.Items.Add(li); } DropDownList4.DataSource = new NationData().Select(); DropDownList4.DataTextField = "NationName"; DropDownList4.DataValueField = "NationCode"; DropDownList4.DataBind(); } Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { 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; bool ok = new UsersDA().Insert(u); //3、提示添加成功 if (ok) { Response.Write("<script>alert('添加成功!')</script>"); Response.Write("<script>this.opener.location.href='Main.aspx';this.close();</script>"); } else { Response.Write("<script>alert('添加失败!')</script>"); } }