内置对象

内置对象:
1、Response - 响应请求对象
Response.Redirect("Default2.aspx"); //重定向
Response.Write("<script>window.open('Default2.aspx');</script>");

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Button1.Click += Button1_Click;//委托写法
        
    }

    //void Button1_Click(object sender, EventArgs e)
    //{
    //    //Response.Redirect("Default2.aspx?aaa="+TextBox1.Text+""); //Redirect 页面重订项。
    //    //Response.Write("hehe");//手动输出
    //    //Response.Write("<script>widow.open('Default2.aspx')</sccript>");//直接在界面上输出内容。
    //}
}

 

 


2、Request - 接收请求对象
Request["键"] - 放在等号右边,用来取值
服务器控 件是根据ID来作为键
表单元素 是根据name值来作为键


QueryString - 页面地址拼接数据
如:Default2.aspx?aaa=呵呵&bbb=嘿嘿&ccc=hoho

1.后台Response.Redirect("Default2.aspx?key="+值);
2.Form表单会通过submit按钮自动提交数据,将数据提交给action设置的页面中去,method=“get”方式来提交

method 提交方式
action 目标页面

get提交方式

post提交方式 - 直接数据提交方式
除了不在地址栏拼接数据,其它的没有任何区别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Text = Request["user"] + "////" + Request["pwd"];
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>

 


===================================================================
3、Session
就是一个临时保存数据的对象,保存在服务器
可以理解为容器,变量

可以存Object类型,一个连接为独立的一组,默认生命周期为20分钟,如果在这20分钟内重新进行数据提交或是页面刷新,都会重置这20分钟倒计时时间。

如果Session["键"]里面没有内容的时候,使用时会抛出异常,所以一定要在使用session的值之前判断此是否为空。

注意:Session不要过度使用,因为放的东西太大了,用户连接多了就会导致内存溢出,服务器崩溃;也不要不使用,会造成资源浪费,因为内存中的数据是最快速的;

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="Default11" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="登陆" /><br />
        <asp:CheckBox ID="CheckBox1" runat="server" Text="7天自动登陆" />
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default11 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "zhangsan" && TextBox2.Text == "123")
        {
            //1、记住当前用户的登陆状态
            Response.Cookies.Add(new HttpCookie("UserName", TextBox1.Text));

            Response.Cookies["Pwd"].Value = TextBox2.Text;

            //2、是否需要长时间保存
            if (CheckBox1.Checked)
            {
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(7);
                Response.Cookies["Pwd"].Expires = DateTime.Now.AddDays(7);
            }
            Response.Redirect("Default12.aspx");
        }
        else
        { 
        
        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default12.aspx.cs" Inherits="Default12" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>cookie使用的主界面</h1>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default12 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;

        if (Request.Cookies["UserName"] != null)
        {
            Label1.Text = Request.Cookies["UserName"].Value + ",欢迎你回来!你的密码是:" + Request.Cookies["Pwd"].Value;
            Label2.Text = "当前版本号:" + Application.Get("aaa").ToString();
        }
        else
        {
            Response.Redirect("Default11.aspx");
        }
    }

    void Button1_Click(object sender, EventArgs e)
    {
        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-2);
        Response.Cookies["Pwd"].Expires = DateTime.Now.AddDays(-2);
        Response.Redirect("Default11.aspx");
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default13.aspx.cs" Inherits="Default13" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default13 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Application["aaa"] = "V1.0";
    }
}

 

4、cookie
临时保存数据的一个对象,保存在客户端
不要保存很重要的信息,因为会随时被用户删掉,有些程序会自动抓取用户电脑上的cookie信息,如果密码保存进去了,就会被抓取到

赋值:使用Response对象
1、Response.Cookies.Add(new HttpCookie("键", 值));

2、Response.Cookies["键"].Value = 值;

3、制作持久Cookie :
Response.Cookies["键"].Expires = DateTime.Now.AddDays(7);//设置7天后过期

取值:使用Request对象
Request.Cookies["UserName"].Value

 

5、Appliction
临时保存数据的一个对象,保存在服务器端
全局变量
所有人访问的都是同一个对象
赋值:
Application["键"] = 值;
取值:
Application.Get("键")

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" method="post" action="Default2.aspx" runat="server">
    <div>
        <asp:TextBox ID="user" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="pwd" runat="server"></asp:TextBox><br />
        <%--?user=zhi&pwd=zhi--%>
        <input type="submit" value="提交" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Application.Get("aaa").ToString();
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim() == "zhangsan" && TextBox2.Text.Trim() == "1234")
        {
            Session["username"] = TextBox1.Text;
            Response.Redirect("Default4.aspx");
        }
        else
        {
            Label1.Text = "用户名密码错误!";
        }

        
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>这是主界面</h1>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:Button ID="Button1" runat="server" Text="退出登陆" />
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
        if (Session["username"] != null)
        {
            Label1.Text = Session["username"].ToString() + ",欢迎回来!";
        }
        else
        {
            Response.Redirect("Default3.aspx");
        }
    }

    void Button1_Click(object sender, EventArgs e)
    {
        Session["username"] = null;
        Response.Redirect("Default3.aspx");
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>这是子界面</h1>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            Label1.Text = Session["username"].ToString() + ",欢迎回来!";
        }
       
    }
}

 

posted on 2016-07-21 08:43  爱意红沉  阅读(143)  评论(0编辑  收藏  举报