一. 使用QueryString變量
QueryString是一種非常簡單也是使用比較多的一種傳值方式,但是它將傳遞的值顯示在瀏覽器的地址欄中,如果是傳遞一個或多個安全性要求不高或是結(jié)構(gòu)簡單的數(shù)值時,可以使用這個方法。
Response.Redirect( "target.aspx?param1=hello¶m2=hi ")
接收頁面: string str = Request.QueryString["param1"];
string str1 = Request.QueryString["param2];
二.使用Cookie對象變量(Cookie是存放在客戶端的)
設(shè)置Cookie: HttpCookie cookie_name = new HttpCookie("name");
cookie_name.Value = Label1.Text;
Reponse.AppendCookie(cookie_name);
獲取Cookie:
string name= Request.Cookie["name"].Value.ToString();
三. 使用session變量(session是存放在服務(wù)器端的)
設(shè)置Session: Session["name"] ="hello";
獲取Session: string name = Session["name"].ToString();
四.使用application 對象變量
Application對象的作用范圍是整個全局,也就是說對所有用戶都有效。此種方法不常使用,因為Application在一個應(yīng)用程序域范圍共享,所有用戶可以改變及設(shè)置其值,故只應(yīng)用計數(shù)器等需要全局變量的地方。
設(shè)置Application : Application["name"] = ="hello";
獲取Application : string name = Application["name"].ToString();
五. PostBackUrl()方法
default.aspx頁面:
Code
1 <asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage" PostBackUrl="~/Default2.aspx" />
2
default2.aspx頁面:
Code
1 if (PReviousPage != null)
2 {
3 TextBox textBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
4 Response.write(textBox1.Text );
5 }
六.使用Server.Transfer方法
這個才可以說是面象對象開發(fā)所使用的方法,其使用Server.Transfer方法把流程從當(dāng)前頁面引導(dǎo)到另一個頁面中,新的頁面使用前一個頁面的應(yīng)答流,所以這個方法是完全面象對象的,簡潔有效。下面這個代碼是展示在需要很多個參數(shù)的時候,使用的方法,如果參數(shù)比較少就沒必要使用這個方法了.
如果讓所有的查詢頁面都繼承一個接口,在該接口中定義一個方法,該方法的唯一作用就是讓結(jié)果頁面獲得構(gòu)建結(jié)果時所需的參數(shù),就可實現(xiàn)多頁面共享一個結(jié)果頁面操作!
新聞熱點
疑難解答