ASP.NET的六大对象介绍

目录

ASP.NET含义:

1.Request对象:

 简单.NET程序示例 1:

示例2 :

2.Response对象:

示例2:

3.Server对象

 示例3:HtmlEncode,HtmlDecode,MapPath

4.Session对象

示例4:

Session练习1:从初始页面传2个整数到Receive.aspx页面进行相加。

session练习2:

Session练习3:购物车

5.Application对象

例子1

6.ViewState对象-暂不介绍

综合示例:聊天室

 


ASP.NET含义:

         ASP(Active Server Pages,活动服务器网页)是Microsoft公司推出的一项动态网页开发技术。       

        ASP既不是一种语言,也不是一种开发工具,而是一种技术框架,其主要功能是把脚本、HTML、组件和Web数据库访问功能有机地结合在一起,形成一个能在服务器端运行的应用程序,该应用程序可根据来自浏览器端的请求生成相应的HTML文档并回送给浏览器。

获取静态网页过程:

 客户端动态网页:

服务器端动态网页:

ASP.NET内置对象:

Request对象

封装了浏览器向Web服务器发送的HTTP请求消息

Response对象

封装了Web服务器向浏览器发送的HTTP响应消息

Server对象

提供对服务器上的方法和属性进行的访问

Session对象

用于存储特定用户会话所需的信息

Application对象

代表运行在Web服务器上的ASP.NET应用程序

ViewState对象

仅在页面提交到服务器之前有效,保存页面的状态信息,如页面当前页码

 

1.Request对象:

      Cookies:获取客户端发送的cookie的集合

      Form:获取窗体变量集合

      QueryString:获取HTTP查询字符串变量集合

   读取单值Cookie:

<%=Request.Cookies["myCookie“]%><br>
<%=Request.Cookies["myCookie“]["Name“]%><br>

 简单.NET程序示例 1:

Default.aspx:

<body>
<form id="form1" runat="server" >
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
     <asp:Button ID="Button1" runat="server"  PostBackUrl="~/Receive.aspx" Text="提交" />        
</form>
</body>

Receive.aspx:

<body>
<form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="传过来的值="></asp:Label> 
    <%=Request.Form["TextBox1"]%>
</form>
</body>

对比以下代码(实现的功能都是一样的):

Default.aspx:

<body>
<form id="form1" runat="server" method="get">
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
     <asp:Button ID="Button1" runat="server"  PostBackUrl="~/Receive.aspx" Text="提交" />        
</form>
</body>

Receive.aspx:

<body>
<form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="传过来的值="></asp:Label> 
    <%=Request.QueryString["TextBox1"]%>
</form>
</body>

=》

例1.1(对比以上例子,主要看是如何传值的)(初学时可忽略下面这个例子)

Default.aspx:

<body>
  <form id="form2" runat="server"  >
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
     <asp:Button ID="Button1" runat="server" OnClick ="Button1_Click" Text="提交" />        
  </form>
</body>

Default.aspx.cs:

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Receive.aspx?param="+TextBox1 .Text );
    }

Receive.aspx:

<body>
  <form id="form1" runat="server">
      <asp:Label ID="Label1" runat="server" Text="传过来的值="></asp:Label> 
      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  </form>
</body>

Receive.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Text = Request.Params["param"];
    }

 =》

示例2 :

Default.aspx

<body>
    <form id="form1" runat="server" method="post">
            <font size="6">登录信息</font><br/>
             用户名<asp:TextBox ID="s_name" runat="server"></asp:TextBox> <br/>
             密 码<asp:TextBox ID="s_password" runat="server" TextMode="Password"/> <br />
         <asp:Button ID="Button1" runat="server" Text="登陆" PostBackUrl="~/Receive.aspx" />
</form>
</body>

 

Receive.aspx

<body>
 <form id="form1" runat="server">
     <% string strname,strpassword;
       strname=Request.Form["s_name"];
       strpassword=Request.Form["s_password"];
      %>
  返回用户注册信息:<br />
  用户名:<%=strname%><br/>
  密  码:<%=strpassword%><br/>
</form>
</body>

=>

2.Response对象:

   Redirect:将客户端重定向到新的URL

   Write:将信息写入HTTP内容输出流

   End:将当前所有缓冲的输出发送到客户端,停止该页的执行

示例2:

Default.aspx

<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <asp:Button ID="NoParamsBtn" runat="server" Text="跳转到Dir.aspx(没有参数)" OnClick="NoParamsBtn_Click"/><br />
                <asp:Label ID="Label1" runat="server" Text="参数:"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                <asp:Button ID="HaveParamsBtn" runat="server" Text="跳转到Dir.aspx(有参数)" OnClick="HaveParamsBtn_Click" />
            </tr>
        </table>
    </form>
</body>

Default.aspx.cs

        protected void NoParamsBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Dir.aspx");
        }

        protected void HaveParamsBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Dir.aspx?param="+Server .UrlEncode (TextBox1 .Text ));
        }

 Dir.aspx

<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="参数:"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </div>
    </form>
</body>

 Dir.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request .Params ["param"]!=null)
            {
                TextBox1.Text = Request.Params["param"];
            }else
            {
                TextBox1.Text = "没有收到数据";
            }
        }

 

3.Server对象

属性名称

说    明

MachineName

获取服务器的计算机名称

ScriptTimeout

获取和设置请求超时

方法名称

说    明

Execute

执行另一个aspx页,执行完该页后再返回本页继续执行

Transfer

终止当前页的执行,并为当前请求开始执行新页

HtmlEncode

对要在浏览器中显示的字符串进行HTML编码并返回已编码的字符串

HtmlDecode

对HTML编码的字符串进行解码,并返回已解码的字符串

MapPath

返回与Web服务器上的指定虚拟路径相对应的物理文件路径

UrlEncode

对URL字符串进行编码,以便通过URL从Web服务器到客户端进行可靠的 HTTP 传输

UrlDecode

对已被编码的URL字符串进行解码,并返回已解码的字符串

UrlPathEncode

对URL字符串的路径部分进行URL编码,并返回已编码的字符串

 示例3:HtmlEncode,HtmlDecode,MapPath

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(Server.MapPath("WebForm1.aspx"));

            string str = "<h1>你好</h1>";
            Response.Write(str);
            string strHtmlEncode = Server.HtmlEncode(str);
            Response.Write(strHtmlEncode + "<br>");
            string strHtmlDecode = Server.HtmlDecode(strHtmlEncode);
            Response.Write(strHtmlDecode + "<br>");

            Response.Write(Request.PhysicalApplicationPath);
        }

4.Session对象

      Session对象代表服务器与客户端所建立的会话.

      从一个客户端打开浏览器并连接到服务器开始,到客户关闭浏览器离开这个服务器结束,被称为一个会话.

      为什么需要Session?当一个客户访问一个服务器时,可能会在这个服务器的多个页面之间反复跳转,服务器应当通过某种办法来识别这是来自同一个客户的不同请求,这种办法通常就是使用session对象。

     session对象可以实现在一个会话期间的多页面间的数据共享/传递。

     可以使用Session 对象存储用户登录网站时候的信息。当用户在页面之间跳转时,存储在Session对象中的变量不会被清除。

     系统为每个访问者都设立一个独立的Session对象,用以存储Session变量,并且各个访问者的Session对象互不干扰。

     Session与Cookie是紧密相关的。 Session的使用要求用户浏览器必须支持Cookie。

     Session主要属性:

          Count:获取会话状态集合中的项数

          Keys:获取存储在会话中的所有值的键的集合

          Timeout:一个Session对象被创建以后,其生存期,默认的时间为20分钟。也可对其修改,如Session.Timeout=60;

示例4:

Session练习1:从初始页面传2个整数到Receive.aspx页面进行相加。

WebForm1.aspx

<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="相加" OnClick="Button1_Click" />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </div>
    </form>
</body>

WebForm1.aspx.cs

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["a"] = TextBox1.Text;
            Session["b"] = TextBox2.Text;
            Response.Redirect("Receive.aspx");
        }

 Receive.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            int a = int.Parse(Session["a"].ToString());
            int b = int.Parse(Session["b"].ToString());
            Response.Write(a + b);
        }

 =>

session练习2:

Default.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["Count"] = 0;
            Session["Name"] = "tom";
            Server.Transfer("WebForm1.aspx");//跳转后不再回来。excute跳转后会回来
        }

WebForm1.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Session.Count; i++)
                Response.Write(Session.Keys[i] + " :"  + Session[i].ToString() + "<br>");
        }

Session练习3:购物车

     见链接:https://blog.csdn.net/qq_40323256/article/details/83934072

5.Application对象

      Application对象是一个比较重要的对象,对Application对象的理解关键是:网站所有的用户公用一个对象,当网站服务器开启的时候,Application就被创建,在网站运行期间持久保存。利用Application这一特性,可以方便地创建聊天室和网站计数器等常用站点应用程序。

     Application对象没有自己的属性,用户可以根据自己的需要定义属性,来保存一些信息,执行完以后,该对象就被保存在服务器上,执行程序时依然可以输出原先保存的值。

<%Application["Greeting“]="你好!"%>
<%=Application["Greeting“]%>

     Application提供两个方法:

(1) Lock()——锁定Application对象,防止被其他用户访问。

(2) Unlock()——解除锁定,可以接受用户的访问。

    Application提供两个事件:

(1) Application_Start()——Application开始创建的时候,调用该事件。

(2) Application_End()——Application被清除的时候,调用该事件。

例子1

Default.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["Count"] == null)
                Application["Count"] = 1;
            else
            {
                int n = (int)Application["Count"];
                n++;
                Application["Count"] = n;
            }
            Response.Write("页面访问量" + Application["Count"]);
        }

6.ViewState对象-暂不介绍

 

综合示例:聊天室

     见链接:https://blog.csdn.net/qq_40323256/article/details/83934458

 

 

 

 

  • 15
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值