(1)Respose對象
利用Response對象輸出文字信息:
PRotected void Page_Load(object sender, EventArgs e)
{ string message = "您好,歡迎光臨本網站!"; Response.Write(message);}
利用Response對象實現網頁跳轉:
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對象傳遞參數:
Response.Resirect("page.aspx?Num=4");
(2)Request對象
QueryString的使用。使用QueryString來獲取從上一個頁面傳遞來的字符串參數。
在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,Form,Cookies,SeverVaiables的順序查找,直到發現了variable所指的關鍵字并返回其值,否則方法返回空值。
Form集合:
Request.Form["Name"].ToString();
ServerVariable集合:
Request.ServerVariable[參數類型];
Cookies集合:
寫入數據:
Response.Cookies[Cookie名稱].Value=數據;
Response.Cookies[Cookie索引號]=數據;
讀取數據:
CookiesValue=Request.Cookies[Cookie名稱].Value;
CookiesValue=Request.Cookies[Cookie索引號].Value;
移出某項數據:
Response.Cookies.Remove("Cookie名稱");
移出所有數據:
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內容</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方法根據URL規則對字符串進行編碼。Server對象的UrlDecode方法根據URL規則對字符串進行解碼。URL規則是當字符串數據以URL的形式傳遞到服務器時,在字符串中不允許出現空格,也不允許出現特殊字符。
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對象計數。
在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("你單間按鈕的次數為:" + ViewState["count"].ToString());}
ViewState對象安全機制:
1.哈希編碼技術。哈希編碼技術被稱為是一種強大的編碼技術。它的算法思想是讓asp.net檢 查ViewState中的所有數據,然后通過散列算法把這些數據編碼。該散列算法產生一段很短的數據 信息,即哈希代碼。然后把這段代碼加在ViewState信息后面。
哈希代碼的的功能實際上是默認的,如果程序員希望有這樣的功能,不需要采用額外的步驟, 但有時開發商選擇禁用此項功能。以防止出現在一個網站系統中不同的服務器有不同的密鑰。為了 禁用哈希代碼,可以在Web.config文件中的<Pages>元素的enableViewStateMac屬性。
<pages enableViewStateMac="false"/>
2.ViewState加密。盡管程序員使用了哈希代碼,ViewState信息依然能夠被用戶閱讀到,很多 情況下這是完全可以接受的。但如果ViewState里包含了需要保密的信息,就需要使用ViewState 加密。
設置單獨某一頁面使用ViewState加密:
<%Page ViewStateEncryptionMode="Always"%>
設置整個網站使用ViewState加密:
<pages viewStateEncryptionMode="Always"/>
使用ViewState對象保留成員變量?;驹恚涸赑age.PreRender事件發生時把成員變量保存在ViewState中,在Page.Load事件發生時取回成員變量。
在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便會被清除。
寫入數據:
Response.Cookies[Cookie名稱].Value=數據;
Response.Cookies[Cookie索引號]=數據;
讀取數據:
CookiesValue=Request.Cookies[Cookie名稱].Value;
CookiesValue=Request.Cookies[Cookie索引號].Value;
移出某項數據:
Response.Cookies.Remove("Cookie名稱");
移出所有數據:
Response.Cookies.Clear();
創建一個cookie實例:
HttpCookie cookie = new HttpCookie("test");//創建一個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對象
讀取數據:
數據=Session[變量名]或數據=Session[索引號]。
寫入數據:
Session[變量名]=數據或Session[索引號]=數據。
移出Session對象中某項數據:
Session.Remove("命名對象");
Session.RemoveAt("命名對象索引");
移出Session對象中的所有數據:
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; //綁定數據到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數量:" + 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.在客戶端存儲。默認狀態下在客戶端使用Cookie存儲Session信息。有時為了防止用戶禁用Cookie造成程序混亂,不使用Cookie存儲Session信息。
2.在服務器端存儲。包括存儲在進程內、存儲在進程外、存儲在SQL Server中。
(7)application對象
Application對象寫入數據格式:
Application[變量名]=數據;
Application[索引號]=數據。
Application對象讀取數據格式:
數據=Application[變量名];
數據=Application[索引號]。
刪除Application對象中的某項數據:
Application.Remove("命名對象");
Application.RemoveAt("命名對象的索引");
移出Application對象中的所有數據:
Application.RemoveAll();
Application.Clear();
加鎖:Application.Lock();解鎖:Application.UnLock();使用時必須成對出現。
使用實例:
在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("訪問次數為:" + Count + "次"); } }
新聞熱點
疑難解答