国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ASP.NET內(nèi)置對象二

2019-11-14 15:47:41
字體:
來源:轉載
供稿:網(wǎng)友

(1)Respose對象

  利用Response對象輸出文字信息:

PRotected void Page_Load(object sender, EventArgs e)
{  string message = "您好,歡迎光臨本網(wǎng)站!";  Response.Write(message);}

  

  利用Response對象實現(xiàn)網(wǎng)頁跳轉:

protected void Page_Load(object sender, EventArgs e){	    Response.Redirect("http://www.easou.com");}

  

  利用response對象像瀏覽器輸出字符串:

protected void Page_Load(object sender, EventArgs e){    Response.Write(@"C:/Users/light/Desktop/Sample/Sample/Default.aspx");}

  利用response對象停止輸出:

protected void Page_Load(object sender, EventArgs e) {    for(int ss=1;ss<30;ss++)    {        Response.Writer(i);        if(i==8)            Response.End();    }}    

  利用Response對象傳遞參數(shù):

Response.Resirect("page.aspx?Num=4");

(2)Request對象

  QueryString的使用。使用QueryString來獲取從上一個頁面?zhèn)鬟f來的字符串參數(shù)。

    在Default.aspx中:

<div>
  <a href="page1.aspx?Num=1&Name=Liu">頁面跳轉</a></div>

    

    在page1.aspx.cs中:

protected void Page_Load(object sender, EventArgs e){    Response.Write("傳遞來的變量值:");    Response.Write("Num的值:" + Request.QueryString["Num"] + "Name的值" + Request.QueryString["Name"]); }

  

  用類似的方法也可以獲取Form、Cookies、SeverVaiables的值。調用方法是:Request.Collection["variable"](variable是要查詢的關鍵字)。Collection包括QueryString、Form、Cookies、SeverVaiables四種集合。使用方式也可以是Request["variable"],與Request.Collection["variable"]的效果一樣。省略了Collection,Request對象就會依照QueryString,F(xiàn)orm,Cookies,SeverVaiables的順序查找,直到發(fā)現(xiàn)了variable所指的關鍵字并返回其值,否則方法返回空值。

  Form集合:

    Request.Form["Name"].ToString();

  ServerVariable集合:

    Request.ServerVariable[參數(shù)類型];

  Cookies集合:

    寫入數(shù)據(jù):

      Response.Cookies[Cookie名稱].Value=數(shù)據(jù);

      Response.Cookies[Cookie索引號]=數(shù)據(jù);

    讀取數(shù)據(jù):

      CookiesValue=Request.Cookies[Cookie名稱].Value;

      CookiesValue=Request.Cookies[Cookie索引號].Value;

  移出某項數(shù)據(jù):

    Response.Cookies.Remove("Cookie名稱");

  移出所有數(shù)據(jù):

    Response.Cookies.Clear();

  Saves的使用(將HTTP請求保存到磁盤):

protected void Page_Load(object sender, EventArgs e){    Request.SaveAs("G:/sample.txt", true);}

 (3)Server對象

  MachineName的使用。獲取本地服務器計算機名稱。

protected void Page_Load(object sender, EventArgs e){    string machineName;    machineName = Server.MachineName.ToString();    Response.Write(machineName);}

  HtmlEncode、HtmlDecode的使用。將<h1>HTML內(nèi)容</h1>編碼后輸出到瀏覽器中,再利用HtmlDecode方法將編碼后的結果還原。

protected void Page_Load(object sender, EventArgs e){    string str1;    str1 = Server.HtmlEncode("<h1>HTML編碼</h1>");//編碼    Response.Write(str1 + "<br/>" + "<br/>");    str1 = Server.HtmlDecode(str1);    Response.Write(str1);}

  URLEncode、UrlDecode的使用。Server對象的URLEncode方法根據(jù)URL規(guī)則對字符串進行編碼。Server對象的UrlDecode方法根據(jù)URL規(guī)則對字符串進行解碼。URL規(guī)則是當字符串數(shù)據(jù)以URL的形式傳遞到服務器時,在字符串中不允許出現(xiàn)空格,也不允許出現(xiàn)特殊字符。

protected void Page_Load(object sender, EventArgs e){    string str2;    str2 = Server.UrlEncode("http://www.easou.com");    Response.Write(str2+"<br/>"+"<br/>");    str2 = Server.UrlDecode(str2);    Response.Write(str2);}

(4)ViewState對象

  使用ViewState對象計數(shù)。

    在Default.aspx中:

<div>    <a href="page1.aspx?Num=1&Name=Liu">頁面跳轉</a>    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"/></div>

    在Default.aspx.cs中:

protected void Button1_Click(object sender, EventArgs e){    int count ;    if (ViewState["count"] == null)        count = 1;    else        count = (int)ViewState["count"] + 1;    ViewState["count"] = count;    Response.Write("你單間按鈕的次數(shù)為:" + ViewState["count"].ToString());}

  ViewState對象安全機制:

    1.哈希編碼技術。哈希編碼技術被稱為是一種強大的編碼技術。它的算法思想是讓asp.net檢 查ViewState中的所有數(shù)據(jù),然后通過散列算法把這些數(shù)據(jù)編碼。該散列算法產(chǎn)生一段很短的數(shù)據(jù) 信息,即哈希代碼。然后把這段代碼加在ViewState信息后面。

      哈希代碼的的功能實際上是默認的,如果程序員希望有這樣的功能,不需要采用額外的步驟, 但有時開發(fā)商選擇禁用此項功能。以防止出現(xiàn)在一個網(wǎng)站系統(tǒng)中不同的服務器有不同的密鑰。為了 禁用哈希代碼,可以在Web.config文件中的<Pages>元素的enableViewStateMac屬性。

      <pages enableViewStateMac="false"/>

    2.ViewState加密。盡管程序員使用了哈希代碼,ViewState信息依然能夠被用戶閱讀到,很多 情況下這是完全可以接受的。但如果ViewState里包含了需要保密的信息,就需要使用ViewState 加密。

      設置單獨某一頁面使用ViewState加密:

        <%Page ViewStateEncryptionMode="Always"%>

      設置整個網(wǎng)站使用ViewState加密:

        <pages viewStateEncryptionMode="Always"/>

  使用ViewState對象保留成員變量。基本原理:在Page.PreRender事件發(fā)生時把成員變量保存在ViewState中,在Page.Load事件發(fā)生時取回成員變量。

    在Default.aspx中:

<table>  <tr>    <td colspan="2">      <asp:TextBox ID="TextBox1" runat="server" Width="100px" BackColor="Beige" />    </td>  </tr>  <tr>    <td>      <asp:Button ID="Button1" runat="server" Width="50px" Text="保存信息" OnClick="Button1_Click"/>    </td>    <td>      <asp:Button ID="Button2" runat="server" Width="50px" Text="讀取信息" OnClick="Button2_Click"/>    </td>  </tr></table>

    在Default.aspx.cs中:

protected string TextBoxContent = "";protected void Page_Load(object sender, EventArgs e){    if (this.IsPostBack)//回送   {        TextBoxContent = ViewState["TextBoxContent"].ToString();    }}protected void Page_PreRender(object sender, EventArgs e){    ViewState["TextBoxContent"] = TextBoxContent;}protected void Button1_Click(object sender, EventArgs e){    TextBoxContent = this.TextBox1.Text.ToString();//存儲文本信息    this.TextBox1.Text = "";//清除信息}protected void Button2_Click(object sender, EventArgs e){    this.TextBox1.Text = TextBoxContent;//讀取TextBoxContent信息}

  頁面間信息傳遞:

    1)跨頁傳遞。

      在Default.aspx中:

<div>    asp:TextBox ID="TextBox1" runat="server" Width="100px" BackColor="Beige" />    <br/>    <asp:Button ID="Button3" runat="server" Text="跨頁傳遞" PostBackUrl="~/page1.aspx"/></div>

      在Default.aspx.cs中:

public string FullContent{    get    {        return this.Title.ToString() + " " + this.TextBox1.Text.ToString();    }}    

      在page1.aspx.cs中:

protected void Page_Load(object sender, EventArgs e){    if (Page.PreviousPage != null)        Response.Write(Page.PreviousPage.Title.ToString());    Default default1=PreviousPage as Default ;    if (default1 != null)        Response.Write("<br/>" + default1.FullContent);//讀取Default屬性值}

    2)使用QueryString

      在Default.aspx中:

<div>        <asp:Button ID="Button1" runat="server" Text="QueryString" OnClick="Button1_Click"/></div>

      在Default.aspx.cs中:

protected void Button1_Click(object sender, EventArgs e){    Response.Redirect("page1.aspx?name=light&age=22");}

      在page1.aspx.cs中:

protected void Page_Load(object sender, EventArgs e){     Response.Write("傳遞過來的信息為:" + "<br>");    Response.Write("name:" + Request.QueryString["name"].ToString() + "<br>");    Response.Write("age:" + Request.QueryString["age"].ToString());}

(5)Cookies對象

  Cookie對象默認有效時間為20分鐘,超過20分鐘Cookie便會被清除。

  寫入數(shù)據(jù):

    Response.Cookies[Cookie名稱].Value=數(shù)據(jù);

    Response.Cookies[Cookie索引號]=數(shù)據(jù);

  讀取數(shù)據(jù):

    CookiesValue=Request.Cookies[Cookie名稱].Value;

    CookiesValue=Request.Cookies[Cookie索引號].Value;

  移出某項數(shù)據(jù):

    Response.Cookies.Remove("Cookie名稱");

  移出所有數(shù)據(jù):

    Response.Cookies.Clear();

  創(chuàng)建一個cookie實例:

HttpCookie cookie = new HttpCookie("test");//創(chuàng)建一個cookie實例cookie.Values.Add("Name", "周周");//添加要存儲的信息,采用鍵/值結合的方式cookie.Expires = DateTime.Now.AddYears(1);Response.Cookies.Add(cookie);//把cookie加入當前的頁面的Respose對象里

  獲取cookie信息:

HttpCookie cookie2 = Request.Cookies["test"];string name1;//聲明一個變量用于存儲cookie信息    if (cookie2 != null)//判斷cookie是否為空    {            name1 = cookie2.Values["Name"];            Response.Write("<br><br>保存的用戶名:" + name1);    }        

  修改cookie值:

    int counter = 0;    if (Request.Cookies["counter"] == null)    {         counter = 0;    }    else    {        counter = counter + 1;    }    Response.Cookies["counter"].Value = counter.ToString();    Response.Cookies["counter"].Expires = System.DateTime.Now.AddDays(1);        

  刪除一個cookie:

            HttpCookie cookie3 = new HttpCookie("test");            cookie3.Expires = DateTime.Now.AddYears(-1);            Response.Cookies.Add(cookie3);

  刪除當前域中的所有cookie:

            HttpCookie cookie4;            int limit = Response.Cookies.Count;            for (int i = 0; i < limit; i++)            {                cookie4= Request.Cookies[i];                cookie4.Expires = DateTime.Now.AddYears(-1);//設置過期              Response.Cookies.Add(cookie4);//覆蓋           }            for (int i = 0; i < limit; i++)//判斷cookie是否為空           {                cookie4 = Request.Cookies[i];                name = cookie4.Values["Name"];                Response.Write("<br><br>保存的用戶名:" + name);            }                

(6)session對象

  讀取數(shù)據(jù):

    數(shù)據(jù)=Session[變量名]或數(shù)據(jù)=Session[索引號]。

  寫入數(shù)據(jù):

    Session[變量名]=數(shù)據(jù)或Session[索引號]=數(shù)據(jù)。

  移出Session對象中某項數(shù)據(jù):

    Session.Remove("命名對象");

    Session.RemoveAt("命名對象索引");

  移出Session對象中的所有數(shù)據(jù):

    Session.RemoveAll();

    Session.Clear();

  應用舉例:

    在Default.aspx中:

    <div>        <table>            <tr>                <td colspan="2" style="height:80px">                    <asp:Label ID="Label1" runat="server"/>                </td>            </tr>            <tr>                <td colspan="2">                    <asp:label ID="Label2" runat="server" Text="請選擇要查看的學生姓名:"/>                </td>            </tr>            <tr>                <td rowspan="2" style="width:200px">                    <asp:ListBox ID="ListBox1" runat="server" Width="100px" Height="100px"/>                </td>                <td style="width:120px;height:20px">                    <asp:Button ID="Button1" runat="server" Text="詳細信息:" OnClick="Button1_Click"/>                </td>            </tr>            <tr>                <td style="width:120px;height:100px">                    <asp:Label ID="Label3" runat="server"/>                </td>            </tr>        </table>    </div>

    在Default.aspx.cs中:

        public class Student        {            public string StuName;            public string StuAge;            public string StuScore;            public Student(string stuName, string stuAge, string stuScore)            {                StuName = stuName;                StuAge = stuAge;                StuScore = stuScore;            }        }        protected void Page_Load(object sender, EventArgs e)        {            if (!this.IsPostBack)            {                //定義Student對象                Student student1 = new Student("李米", "24", "89");                Student student2 = new Student("劉宇", "22", "95");                Student student3 = new Student("吳雅", "23", "86");                //Session存儲studnet信息                Session["student1"] = student1;                Session["student2"] = student2;                Session["student3"] = student3;                //綁定數(shù)據(jù)到ListBox                this.ListBox1.Items.Clear();                this.ListBox1.Items.Add(student1.StuName);                this.ListBox1.Items.Add(student2.StuName);                this.ListBox1.Items.Add(student3.StuName);            }            //顯示Session信息            this.Label1.Text = "SessionId:" + Session.SessionID.ToString() + "<br>";            this.Label1.Text += "Session數(shù)量:" + Session.Count.ToString() + "<br>";            this.Label1.Text += "Session模式:" + Session.Mode.ToString() + "<br>";            this.Label1.Text += "Session有效期:" + Session.Timeout.ToString();        }        protected void Button1_Click(object sender, EventArgs e)        {            if (this.ListBox1.SelectedIndex == -1)                this.Label1.Text = "";            else            {                //獲取Session鍵值                string key = "student" + (this.ListBox1.SelectedIndex + 1).ToString();                //獲取student對象                Student student = (Student)Session[key];                //顯示學生信息                this.Label3.Text += "學生姓名:" + student.StuName + "<br>";                this.Label3.Text += "學生年齡:" + student.StuAge + "<br>";                this.Label3.Text += "學生成績:" + student.StuScore;            }        }

  Session存儲。

    1.在客戶端存儲。默認狀態(tài)下在客戶端使用Cookie存儲Session信息。有時為了防止用戶禁用Cookie造成程序混亂,不使用Cookie存儲Session信息。

    2.在服務器端存儲。包括存儲在進程內(nèi)、存儲在進程外、存儲在SQL Server中。

(7)application對象

  Application對象寫入數(shù)據(jù)格式:

    Application[變量名]=數(shù)據(jù);

    Application[索引號]=數(shù)據(jù)。

  Application對象讀取數(shù)據(jù)格式:

    數(shù)據(jù)=Application[變量名];

    數(shù)據(jù)=Application[索引號]。

  刪除Application對象中的某項數(shù)據(jù):

    Application.Remove("命名對象");

    Application.RemoveAt("命名對象的索引");

  移出Application對象中的所有數(shù)據(jù):

    Application.RemoveAll();

    Application.Clear();

  加鎖:Application.Lock();解鎖:Application.UnLock();使用時必須成對出現(xiàn)。

  使用實例:

    在Global.asax.cs中:

        protected void Application_Start(object sender, EventArgs e)        {            //應用程序啟動時運行的代碼            Application["count"] = 0;        }        protected void Session_Start(object sender, EventArgs e)        {            //新會話啟動時運行的代碼            Application.Lock();            int count = Convert.ToInt32(Application["count"]);            Application["count"] = count + 1;            Application.UnLock();        }

    在Default.aspx.cs中:

        protected void Page_Load(object sender, EventArgs e)        {            //第一次加載頁面            if (!IsPostBack)            {                string Count = Application["count"].ToString();                Response.Write("訪問次數(shù)為:" + Count + "次");            }        }    

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 麦盖提县| 章丘市| 樟树市| 奉新县| 马边| 南丹县| 三台县| 监利县| 巴楚县| 凤台县| 丰顺县| 南雄市| 綦江县| 孝昌县| 庆云县| 临邑县| 霍林郭勒市| 灵璧县| 石河子市| 南皮县| 浦北县| 诸暨市| 堆龙德庆县| 昭觉县| 运城市| 射洪县| 嘉峪关市| 鄂托克旗| 卓尼县| 台北市| 吴堡县| 临沧市| 香港| 梁河县| 文昌市| 兴文县| 志丹县| 石景山区| 烟台市| 新蔡县| 武隆县|