首页 技术 正文
技术 2022年11月8日
0 收藏 766 点赞 1,394 浏览 11320 个字

一、内置对象

(一)Response对象

1、简介:response 对象在ASP中负责将信息传递给用户.Response对象用于动态响应客户端请求,并将动态生成的响应结果返回到客户端浏览器中,使用Response对象可以直接发送信息给浏览器,重定向浏览器到另一个URL或设置cookie的值等.

2、方法:①、write方法:response.write **

                             功能:向客户端发送浏览器能够处理的各种数据,包括:html代码,脚本程序等.                             实例:response.write “I LOVE YOU !!”               ②、redirect方法:response.redirect(“url”)的作用是在服务器端重定向于另一个网页。 


(二)Request对象1、简介:Request对象的作用是与客户端交互,收集客户端的Form、Cookies、超链接,或者收集服务器端的环境变量。               request对象是从客户端向服务器发出请求,包括用户提交的信息以及客户端的一些信息。客户端可通过HTML表单或在网页地址后面提供参数的方法提交数据,然后通过request对象的相关方法来获取这些数据。request的各种方法主要用来处理客户端浏览器提交的请求中的各项参数和选项。2、Request对象的五个集合:①、QueryString:用以获取客户端附在url地址后的查询字符串中的信息。

                                                                          例如: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中数据进行增删改 主页前台代码:

    </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(" rel="external nofollow" UserName") %>" onclick="Del" >删除</a>
<a href="Update.aspx?un=<%#Eval(" rel="external nofollow" 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(-);
//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前台

Add页面后台代码:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ for (int i = DateTime.Now.Year; i >= ; i--)
{
ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList1.Items.Add(li);
} for (int i = ; i <= ; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList2.Items.Add(li);
} for (int i = ; i <= ; 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页面中点击“”“添加”按钮,会提示添加是否成功,若添加成功,关闭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");
}

Delete后台

注:在这里用到了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>

js密码

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>");
}

修改

     

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295