zoukankan      html  css  js  c++  java
  • 【总结——ASP.NET对象】

    总结——ASP.NET 对象

    ASP.NET六大对象是ASP.NET的基础,也是重要的知识,现总结如下。

     

    Response对象

     

    Request对象

    理解:

    1. 对于get方式的提交,用Request.QueryString["param"]方式获取浏览器参数

    如:

    URL:

    <a href="test.aspx?name=Frankie&sex=">URL1</a>

     

    string name = Request.QueryString["name"];

    string sex = Request.QueryString["sex"];

     

    1. 对于post方式提交的表单,用Request.Form["name"]方式获取表单提交的值

    如:

    表单:

    <form action="1.aspx" method="post" id="form1">

    <p>姓名<input type="text" size="20" name="Name" /></p>

    <p>兴趣<input type="text" size="20" name="Love" /></p>

    <p><input type="submit" name="B1" value="提交" /></p>

    </form>

     

    string name = Request.Form["Name"];

    string hobby = Request.Form["Hobby"];

     

     

    Application对象

    理解关键:所有用户都共用一个Application对象,当网站服务器一开,就创建了Application对象,所有的用户都可以对Application对象进行修改。

    应用:聊天室、网页计数器等。

     

    聊天室

    登录界面(前台代码login.aspx):

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" action="chatMain.aspx" method="post" runat="server">

    <div>

    请输入大名:<input type="text" size="10" value="采花大盗" name="username" />

    请输入帐号:<input type="text" size="8" value="123" name="no"/>

    <input type="submit" name="submit" size="8" value="提交" />

    </div>

    </form>

    </body>

    </html>

     

     

    登录(后台代码login.aspx.cs):

     

    protected void Page_Load(object sender, EventArgs e)

    {

    if (Request["submit"] == "提交")

    {

    //Response.Write(Request["submit"]);

    Session["username"] = Request["username"];

    Session["no"]=Request["no"];

    Response.Redirect("../Application/chatpage.htm");

    }

    }

     

     

     

    显示界面(Chat.html):

    分为上下两个框架,一个用来显示聊天内容,一个用来发送信息。模拟QQ聊天界面。

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>Chat Page</title>

    </head>

    <frameset rows="*,100">

    <frame src="Display.aspx" />

    <frame src="Message.aspx" />

    </frameset>

    </html>

     

    发送消息窗口(Message.aspx):

     

    <head runat="server">

    <title></title>

    </head>

    <body bgcolor="lightblue">

    <form id="form1" action="Message.aspx" method="post" runat="server">

    <div>

    <input type="text" name="message" size="50" />

    <input type="submit" value="send" />

    </div>

    </form>

    </body>

    </html>

     

    后台代码(Message.aspx.cs):

    这是最重要的地方。

     

    protected void Page_Load(object sender, EventArgs e)

    {

    string username = Session["username"].ToString();

    string no = Session["no"].ToString();

    string mywords = Request["mywords"];

    string talk = Request["message"];

    mywords = "姓名:" + username + " 机器号?" + no + "说:" + talk;

    Application.Lock();

    Application["talk"] = Application["talk"] + mywords + "<br/>";

    Application.UnLock();

    }

     

    显示消息界面(Display.aspx):

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title>Display</title>

    <meta http-equiv="Refresh" content="5"; url="Display.aspx" />

    <script type="text/javascript">

    function scrollWindow() {

    this.scroll(0, 65000);

    setTimeout("scrollWindow()", 200);

    }

    scrollWindow();

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <p align="right"></p>

    <% Response.Write(Application["talk"]); %>

    </form>

    </body>

    </html>

     

    完毕!

     

     

    自制网页计数器

    前台页面(counter.aspx):

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <p>你是本站第<h1><%=GetImg(Convert.ToInt32(Application["counter"])) %></h1>位访客!</p>

    </body>

    </html>

     

     

    后台页面(counter.aspx.cs):

    很重要。

     

    protected void Page_Load(object sender, EventArgs e)

    {

    Application.Lock();

    //Add 1 when every page_load

    Application["counter"] = Convert.ToInt32(Application["counter"]) + 1;

    Application.UnLock();

    }

     

    /// <summary>

    /// help method, transform a number to .gif

    /// </summary>

    public string GetImg(int counter)

    {

    string myimage = string.Empty;

    string S = counter.ToString();

     

    for (int i = 0; i <= S.Length - 1; i++)

    {

    myimage = myimage + "<img src='/Application/img/" + S.Substring(i, 1) + ".gif" + "' />";

    }

     

    return myimage;

     

    }

     

    完毕!

     

    Server对象

    Server对象提供对服务器上的方法和属性进行访问。例如对URL或者HTML编码成字符串,获奖虚拟路径映射到物理路径以及设置脚本的超时期限等。

    向浏览器输出HTML代码:

    protected void Page_Load(object sender, EventArgs e)

    {

    Response.Write(Server.MachineName+Server.HtmlEncode(" Hello world;(<h1>Welcome.</h1>)"));

    Response.Write("<br />");

    Response.Write("Hello world;(<h1>Welcome.</h1>)");

    }

     

    显示效果如下:

     

    取得文件路径:

    Server.MapPath()的用途是把"网络路径"转换为Server机器上的实际路径。

     

    GetMapPath.aspx.cs

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    Server.MapPath("/") 传回:<%=Server.MapPath("/") %>

    <br />

    Server.MapPath("/abc.txt") 传回:<%=Server.MapPath("/abc.txt") %>

    <br />

    </div>

    </form>

    </body>

    </html>

     

     

    Session对象

    Session其实就是访问者从到达某个特定网页到离开为止的那段时间,每个访问者都会获得一个单独的Session。

     

    用Session实现电子商务网站购物车功能

    网络购物车

    肉店:meatShop.aspx

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" action="meatShop.aspx" method="post" runat="server">

    <h1><p>各种肉大甩卖,一律十块:</p></h1>

    <p><input type="checkbox" name="pork" value="pork" />Pork</p>

    <p><input type="checkbox" name="beef" value="beef" />Beef</p>

    <p><input type="checkbox" name="mutton" value="mutton" />Mutton</p>

    <p><input type="submit" value="Submit" name="submit" />

    <input type="reset" value="Reset" name="reset" />

    <a href="ballShop.aspx">Buy Another</a>

    <a href="Cart.aspx">Look up Cart</a>

    </p>

    </form>

    </body>

    </html>

     

    后台代码:meatShop.aspx.cs

    protected void Page_Load(object sender, EventArgs e)

    {

    if (Request["submit"] == "Submit")

    {

    Session["pork"] = Request["pork"];

    Session["beef"] = Request["beef"];

    Session["mutton"] = Request["mutton"];

    }

    }

     

     

    球店:ballShop.aspx

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" action="ballShop.aspx" method="post" runat="server">

    <h1><p>各种球大甩|卖,一律8块:</p></h1>

    <p><input type="checkbox" name="basketball" value="basketball" />basketball</p>

    <p><input type="checkbox" name="football" value="football" />football</p>

    <p><input type="checkbox" name="volleyball" value="volleyball" />volleyball</p>

    <p><input type="submit" name="submit" value="submit" />

    <input type="reset" value="reset" name="reset" />

    <a href="meatShop.aspx">Buy Another</a>

    <a href="Cart.aspx">查看购物车</a>

    </p>

    </form>

    </body>

    </html>

     

     

    后台代码:ballShop.aspx.cs

    protected void Page_Load(object sender, EventArgs e)

    {

            if (Request["submit"] == "submit")

            {

                Session["basketball"] = Request["basketball"];

                Session["football"] = Request["football"];

                Session["volleyball"] = Request["volleyball"];

    }

    }

     

     

    购物车:Cart.aspx

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title>User Name</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <h1><p>你选择的结果是:</p></h1>

    <div align="center">

    <center>

    <%Response.Write(Session["pork"]);%><br />

    <%Response.Write(Session["beef"]);%><br />

    <%Response.Write(Session["mutton"]);%><br />

    <%Response.Write(Session["basketball"]);%><br />

    <%Response.Write(Session["football"]);%><br />

    <%Response.Write(Session["volleyball"]);%>

    </center>

    </div>

    </form>

    </body>

    </html>

     

     

    Cookie对象

    写入Cookie:writeCookie.aspx.cs

    protected void Page_Load(object sender, EventArgs e)

    {

            HttpCookie myCookie = new HttpCookie("user");

            myCookie.Value = "Hello World";

            Response.Cookies.Add(myCookie);

            //Response.Write(myCookie.Value);

    }

     

    读取Cookie:readCookie.aspx.cs

    protected void Page_Load(object sender, EventArgs e)

    {

            string myCookie = Request.Cookies["user"].Value;

            Response.Write(myCookie);

    }

     

     

    完毕!

  • 相关阅读:
    UVALive 7141 BombX
    CodeForces 722D Generating Sets
    CodeForces 722C Destroying Array
    CodeForces 721D Maxim and Array
    CodeForces 721C Journey
    CodeForces 415D Mashmokh and ACM
    CodeForces 718C Sasha and Array
    CodeForces 635C XOR Equation
    CodeForces 631D Messenger
    田忌赛马问题
  • 原文地址:https://www.cnblogs.com/fanyong/p/2215914.html
Copyright © 2011-2022 走看看