• web窗体的内置对象


    内置对象:
    1、Response - 响应请求对象
    Response.Redirect("Default2.aspx"); //重定向
    Response.Write("<script>window.open('Default2.aspx');</script>"); ---可以书写任何东西,直接输出出去

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


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

    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)
        {
            Response.Write(Request["aaa"]);
        }
    }

    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);
            //Response.Write("呵呵");
            
        }
    }
    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["aaa"];
        }
    }



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

    method 提交方式
    action 目标页面

    get提交方式

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default1.aspx.cs" Inherits="Default1" EnableViewStateMac="false" %>
    
    <!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="get" action="Default2.aspx" runat="server">
            <asp:TextBox ID="User" runat="server"></asp:TextBox><br />
            <asp:TextBox ID="Pwd" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="Button" />
            
        </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 Default1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        
        }
    
     
    }
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" EnableViewStateMac="false" %>
    
    <!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>
        </form>
    </body>
    </html>
    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"];
        }
    }



    post提交方式 - 直接数据提交方式
    除了不在地址栏拼接数据,其它的没有任何区别
    ===================================================================
    3、Session
    就是一个临时保存数据的对象,保存在服务器
    可以理解为容器,变量

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

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

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

    <%@ 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">
            用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            密码:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="登录" /><asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </form>
    </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()=="123")
            {
            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">
           这是session的主页面<br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    </html>
    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)
        {
            if (Session["username"] != null)
            {
                Label1.Text = Session["username"] + ",欢迎登录!";
            }
            else
            {
                Response.Redirect("Default3.aspx");
            }
        }
    }

    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

    <%@ 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">
            用户名:<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天自动登录"/>
            <asp:Label ID="Label1" runat="server" Text=""></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 Default5 : 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")
            {
                Response.Cookies["username"].Value = TextBox1.Text;
                Response.Redirect("Default6.aspx");
                if(CheckBox1.Checked)
                {
                    Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);
                }
            }
            else
            {
                Label1.Text = "用户名密码错误!";
            }
        }
    }
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
    
    <!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">
          这是cookie的主页面<br />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
            <asp:Button ID="Button1" runat="server" Text="退出" />
    
        </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 Default6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
            if (Request.Cookies["username"].Value!=null)
            {
            Label1.Text = Request.Cookies["username"].Value + ",欢迎登录";
            }
            else
            {
                Response.Redirect("Default5.aspx");
            }
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            Response.Cookies["username"].Expires = DateTime.Now.AddDays(-2);
            Response.Redirect("Default5.aspx");
        }
    }

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

  • 相关阅读:
    Vim基本功
    八个最常用的正则表达式
    程序员总结:帮助你早些明白一些道理
    HttpClient
    Red.Hat.Enterprise.Linux.6.2下安装vim 、OpenOffice、JDK、Eclipse
    输出打印某个对象所有属性及属性值
    Notepad++
    写博客?
    解决操作WordPress时提示输入FTP信息
    JS通过键盘点击事件实现div移动
  • 原文地址:https://www.cnblogs.com/fengsantianya/p/5681623.html
Copyright © 2020-2023  润新知