Webform(五)——内置对象(Response、Request…
2018-06-22 07:37:31来源:未知 阅读 ()
一、内置对象
(一)Response对象
1、简介:response 对象在ASP中负责将信息传递给用户.Response对象用于动态响应客户端请求,并将动态生成的响应结果返回到客户端浏览器中,使用Response对象可以直接发送信息给浏览器,重定向浏览器到另一个URL或设置cookie的值等.
2、方法:①、write方法:response.write **
(二)Request对象
●不要传过长东西,因为长度有限,过长会造成数据丢失。
</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>"); } }
注:在Add页面中点击“”“添加”按钮,会提示添加是否成功,若添加成功,关闭Add页面,刷新Main主页面。在这里用到了Response对象中的write方法!
(二)、删除
Delete页面前台代码:无
Delete页面后台代码:
protected void Page_Load(object sender, EventArgs e) { //1、获得要删除的主键值,username string Uname = Request["un"].ToString(); //2、删除 new UsersDA().Delete(Uname); //3、调回显示页面 Response.Redirect("Main.aspx"); }
注:在这里用到了Response对象中的Redirect方法和Request对象中的QueryString集合,当然,在用QueryString集合时,需要Main主页里面写传递,这个可以见上面的Main主页前台代码。
(三)、修改
Update前台代码:
<form id="form1" runat="server"> <h1>用户修改</h1> 用户名:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><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">男</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>
Update后台代码:
//建一个变量来存储原密码
string pwd = "";
protected void Page_Load(object sender, EventArgs e)
{
//1、将传过来的主键值接收
string uname = Request["un"].ToString();
//2、通过主键值将对象查出来
Users u = new UsersDA().Select(uname);
pwd = u.PassWord;
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();
//3、将对象中的数据绑定到每一个控件上去
Label2.Text = u.UserName;
TextBox4.Text = u.NickName;
foreach (ListItem li in RadioButtonList1.Items)
{
if (u.Sex)
{
if (li.Value == "True")
{
li.Selected = true;
}
}
else
{
if (li.Value == "False")
{
li.Selected = true;
}
}
}
DropDownList1.SelectedValue = u.Birthday.Year.ToString();
DropDownList2.SelectedValue = u.Birthday.Month.ToString();
DropDownList3.SelectedValue = u.Birthday.Day.ToString();
DropDownList4.SelectedValue = u.Nation;
}
Button1.Click += Button1_Click;
}
void Button1_Click(object sender, EventArgs e)
{
//1、构建一个Users对象
Users u = new Users();
u.UserName = Label2.Text;
//获取密码
if (TextBox2.Text == "" && TextBox3.Text == "")
{
u.PassWord = pwd;
}
else
{
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 UsersDA().Update(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>");
}
注:●在这里用到了Response对象中的write方法和Request对象中的QueryString集合,当然,在用QueryString集合时,需要Main主页里面写传递,这个可以见上面的Main主页前台代码。
●点击“”修改“”按钮,弹提示框,若添加成功,关闭Update页面,刷新Main主页面。
后注:部分重要代码
1、js代码判断两次密码是否一致
<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>
2、弹框显示添加/修改是否成功,成功则刷新主页面reparter数据
※this.opener.location.href='Main.aspx':跨界面刷新主页面
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>"); }
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>"); }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Asp.Net中WebForm的生命周期 2020-03-29
- ASP.NET使用Ajax返回Json对象的方法 2020-03-23
- 详解ABP框架中的数据过滤器与数据传输对象的使用 2020-01-18
- ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中 2019-08-13
- 【转载】 C#中手动创建一个DataTable对象并写入数据 2019-07-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash