ASP.NET 内置对象

1. Response 对象
Response对象是HttpResponse类的一个实例, 它用于控制服务器发送给浏览器的信息, 包括直接发送信息给浏览器, 重定向浏览器到另一个URL或设置cookie的值.
①. 发送信息
Response.Write(value);
②. 重定向浏览器
Response.Redirect(Url);
2. Request 对象
Request对象是HttpRequest类的一个实例, 其主要功能是从客户端获取数据.
①. 获取URL传递变量
Request对象的QueryString属性可以用来获取URL地址中"?"后面的数据, 即URL附加信息.
QueryString主要用于获取HTTP协议中GET请求发送的数据. Get方式是HTTP请求中默认的请求方式.

Request :

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Request对象</title>
</head>
<body>
<center>
    
<form id="form1" runat="server" action="incept.aspx" method="get">
        请输入你的姓名
<input type="text" name="name" /><p></p>
        
<input type="submit" value="发送" />
    
</form>
</center>
</body>
</html>

incept

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Request对象</title>
</head>
<body>
<center>
    
<form id="form1" runat="server">
    
<h4>接受Get方法传递来的数据<br /></h4>
    
<%
        
string name = Request.QueryString["name"];
        Response.Write(
"你的姓名为" + name);
         
%>
    
</form>
</center>
</body>
</html>

②. 获取表单传递值
当需要在网页间传递信息时, 还可以通过表单来实现, 表单传递的信息可以由Request对象的Form属性来获取.
Request

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Request对象</title>
</head>
<body>
    
<form id="form1" runat="server" action="formadd.aspx" method="post">
    请输入你的姓名:
<input type="text" name="name" /><br />
    请输入你的性别:
<input type="text" name="sex" /><br />
    请输入你的职业:
<input type="text" name="occupation" /><br />
    
<input type="submit" value="发送" id="submit1" name="submit1" />
        
    
</form>
</body>
</html>

Form

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Request对象2</title>
</head>
<body>
<center>
    
<form id="form2" runat="server">
    
<h4>接受POST方法所传的数据<br /></h4>
    
<%
        
string name = Request.Form["name"];
        Response.Write(
"你的姓名为:" + name + "<br>");

        
string sex = Request.Form["sex"];
        Response.Write(
"你的性别为:" + sex + "<br>");

        
string occupation = Request.Form["occupation"];
        Response.Write(
"你的职业为:" + occupation + "<br>");
         
%>
    
</form>
</center>
</body>
</html>

 3. Server对象
Server对象提供了对服务器上方法和属性的访问.
Server对象的大多数方法和属性是作为实用程序的功能服务的, 例如, 使用它们可以实现转变字符串格式, 创建捆绑对象以及控制页面显示时间等.
①. 向浏览器输出HTML代码
使用Server对象的HTMLEncode方法可以向浏览器输出HTML代码.

HTMLCode

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Server对象</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<%
        Response.Write(Server.HtmlEncode(
"<center><h4>成功属于,永不放弃的人!<br></h4></center>"));
        Response.Write(
"<center><h4>成功属于,永不放弃的人!<br></h4></center>");
         
%>
    
</form>
</body>
</html>

4. Application对象
Application对象是运行在Web应用服务器上的虚拟目录及其子目录下所有文件,页面,模块和可执行代码的总和.
①. 使用Application对象的自定义属性
可以根据特定的需要为Application对象定义属性, 以存储一些公有的数据, 语法:
Application["属性名"]
示例:

Application

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Application对象</title>
</head>
<body>
<center>
    
<form id="form1" runat="server">
        
<%
            Application[
"regardMorning"= "上午好!";
            Application[
"regardAfernoon"= "下午好!";
            Application[
"regardNight"= "晚上好!";
         
%>
         
<%=Application["regardMorning"%><br /><p></p>
         
<%=Application["regardAfernoon"]%><br /><p></p>
         
<% =Application["regardNight"%>
    
</form>
</center>
</body>
</html>

②. 网页计数器
网页计数器是Application对象的功能之一, 由于Application对象是所有用户共享的, 因而可以用来存储计数器的值, 当有新用户访问网页时, 可以自动增加计数器的值.

Count

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Application对象</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<%
            Application.Lock();
            Application[
"count"= Convert.ToInt32(Application["count"]) + 1;
            Application.UnLock();
             
%>
        
<p align="center">您是本网站第<%=Application["count"%>位贵宾!</p>
    
</form>
</body>
</html>

5. Session对象
从一个客户到达某个网站开始, 到其离开该网页为止的这段时间内, 服务器会为该用户分配一个Session, 以保存该用户会话时所需要的信息.
①. 利用Session的自定义属性保存信息
Session对象的主要用途是保存信息. 当一个客户第一次登录网站时,系统会为其分配一个Session, 只有当该客户退出时,或Session的生命周期结束时, 信息才会被清除.
②. Session的唯一性和Session的终止
当客户登录网站后, 服务器会为其分配一个Session, 不同客户的Session是各不相同的, 用以标识不同的客户, SessionID属性是用以区别Session的唯一标志, 每个Session都具有唯一的SessionID.
③. 利用Session实现购物车
利用Session还可以创建虚拟购物车, 当客户在网络商店中选择商品时, 该商品就会进入购物车里面, 客户的购物信息就保存在Session中.
Session的一个重要应用就是实现电子商务网站的购物车.
模拟购物车实例:
购买商品

 Product

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Session对象</title>
</head>
<body>
<%
    
if (Request["b1"== "提交")
    {
        Session[
"s1"= Request["c1"];
        Session[
"s2"= Request["c2"];
        Session[
"s3"= Request["c3"];
    }
     
%>
     各种彩电大减价,欢迎选购
!
    
<form id="form1" runat="server" method="post" action="Product.aspx">
    
<p><input type="checkbox" name="c1" value="海尔彩电"/>海尔彩电</p>
    
<p><input type="checkbox" name="c2" value="长虹彩电"/>长虹彩电</p>
    
<p><input type="checkbox" name="c3" value="康佳彩电"/>康佳彩电</p>
    
<p><input type="submit" name="b1" value="提交" />
    
<input type="reset" name="b2" value="全部重写" />
    
<a href="otherProduct.aspx">买其他商品</a>
    
<a href="myCart.aspx">查看购物车</a>
    
</p>
    
</form>
</body>
</html>

 购买其他商品:

otherProduct

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Session对象</title>
</head>
<body>
<%
    
if (Request["x1"== "提交")
    {
        Session[
"s4"= Request["b1"];
        Session[
"s5"= Request["b2"];
        Session[
"s6"= Request["b3"];
    }
     
%>
     各种鞋子大减价,欢迎选购
!
    
<form id="form1" runat="server" method="post" action="otherProduct.aspx">
    
<p><input type="checkbox" name="b1" value="旅游鞋" />旅游鞋</p>
    
<p><input type="checkbox" name="b2" value="跑鞋" />跑鞋</p>
    
<p><input type="checkbox" name="b3" value="冰鞋" />冰鞋</p>
    
<p><input type="submit" name="x1" value="提交" />
    
<input type="reset" name="b2" value="全部重写" />
    
<a href="Product.aspx">买其他商品</a>
    
<a href="myCart.aspx">查看购物车</a>
    
</p>
    
</form>
</body>
</html>

查看购物车:

Cart

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>使用Session对象</title>
</head>
<body>
购物情况显示:
    
<div align="center">
    
<%
        Response.Write(Session[
"s1"+ "<br>");
        Response.Write(Session[
"s2"+ "<br>");
        Response.Write(Session[
"s3"+ "<br>");
        Response.Write(Session[
"s4"+ "<br>");
        Response.Write(Session[
"s5"+ "<br>");
        Response.Write(Session[
"s6"+ "<br>");
         
%>
    
</div>
</body>
</html>

6. Cookie对象
在HTTP协议下, Cookie只不过是一个文本文件, 是服务器或者脚本用以维护用户信息的一种方式, Cookie可以用来记录用户的相关信息.
向浏览器输出Cookie, 也就是将Cookie写入到浏览器中, 让浏览器保存Cookie的值.

Cookie

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Cookie : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie myCookie 
= new HttpCookie("user");
        myCookie.Value
="You are really wonderful!";
        Response.Cookies.Add(myCookie);
        Response.Write(
"<center>" + "写入Cookie" + "</center>");

        
string aCookie = Request.Cookies["user"].Value;
        Response.Write(
"<center>" + aCookie + "</center>");
        Response.Write(
"<br>");
    }
}

 文章转自:http://www.cnblogs.com/huakai/archive/2009/09/29/1575467.html

posted @ 2009-09-29 18:26  Sissynong  阅读(266)  评论(0编辑  收藏  举报