免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
ASP.NET頁(yè)面間傳值的幾種方式

以下關(guān)于Asp.net 頁(yè)面間傳值的幾種方式的介紹是引用的,感覺(jué)挺好跟大家一起分享:

1。使用QueryString
      使用QuerySting在頁(yè)面間傳遞值已經(jīng)是一種很老的機(jī)制了,這種方法的主要優(yōu)點(diǎn)是實(shí)現(xiàn)起來(lái)非常簡(jiǎn)單,然而它的缺點(diǎn)是傳遞的值是會(huì)顯示在瀏覽器的地址欄上的(不安全),同時(shí)又不能傳遞對(duì)象,但是在傳遞的值少而安全性要求不高的情況下,這個(gè)方法還是一個(gè)不錯(cuò)的方案。使用這種方法的步驟如下:
1,使用控件創(chuàng)建web表單(form)
2,創(chuàng)建可以返回表單的按鈕和鏈接按鈕
3,在按鈕或鏈接按鈕的單擊事件里創(chuàng)建一個(gè)保存URL的字符變量
4,在保存的URL里添加QueryString參數(shù)
5,使用Response.Redirect重定向到上面保存的URL
下面的代碼片斷演示了如何實(shí)現(xiàn)這個(gè)方法:
  源頁(yè)面WebForm1.aspx.cs中的部分代碼:
private void Button1_Click(object sender, System.EventArgs e)
{
     string url;
     url="WebForm2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
     Response.Redirect(url);
}
 目標(biāo)頁(yè)面WebForm2.aspx.cs中的部分代碼:
private void Page_Load(object sender, System.EventArgs e)
{
     Label1.Text=Request.QueryString["name"];
     Label2.Text=Request.QueryString["email"];
}


2。使用Session變量
      使用Session變量是可以在頁(yè)面間傳遞值的的另一種方式,在本例中我們把控件中的值存在Session變量中,然后在另一個(gè)頁(yè)面中使用它,以不同頁(yè)面間實(shí)現(xiàn)值傳遞的目的。但是,需要注意的是在Session變量存儲(chǔ)過(guò)多的數(shù)據(jù)會(huì)消耗比較多的服務(wù)器資源,在使用session時(shí)應(yīng)該慎重,當(dāng)然了,我們也應(yīng)該使用一些清理動(dòng)作來(lái)去除一些不需要的session來(lái)降低資源的無(wú)謂消耗。使用Session變量傳遞值的一般步驟如下:
1,在頁(yè)面里添加必要的控件
2,創(chuàng)建可以返回表單的按鈕和鏈接按鈕
3,在按鈕或鏈接按鈕的單擊事件里,把控件的值添加到session變量里
4,使用Response.Redirect(或Server.Transfer)方法重定向到另一個(gè)頁(yè)面
5,在另一個(gè)頁(yè)面提取session的值,在確定不需要使用該session時(shí),要顯式清除它
下面的代碼片斷演示了如何實(shí)現(xiàn)這個(gè)方法:
   源頁(yè)面WebForm1.aspx.cs中的部分代碼:
private void Button1_Click(object sender, System.EventArgs e)
{
     //textbox1 and textbox2 are webform
     //controls
     Session["name"]=TextBox1.Text;
     Session["email"]=TextBox2.Text;
     Server.Transfer("WebForm2.aspx");
}
  目標(biāo)頁(yè)面WebForm2.aspx.cs中的部分代碼:
private void Page_Load(object sender, System.EventArgs e)
{
     Label1.Text=Session["name"].ToString();
     Label2.Text=Session["email"].ToString();
     Session.Remove("name");
     Session.Remove("email");
}

3.使用Server.Transfer
      這個(gè)方法相比上面介紹的方法稍微復(fù)雜一點(diǎn),但在頁(yè)面間值傳遞中卻是特別有用的,使用該方法你可以在另一個(gè)頁(yè)面以對(duì)象屬性的方式來(lái)存取顯露的值,當(dāng)然了,使用這種方法,你需要額外寫一些代碼以創(chuàng)建一些屬性以便可以在另一個(gè)頁(yè)面訪問(wèn)它,但是,這個(gè)方式帶來(lái)的好處也是顯而易見(jiàn)的??傮w來(lái)說(shuō),使用這種方法是簡(jiǎn)潔的同時(shí)又是面向?qū)ο蟮?。使用這種方法的整個(gè)過(guò)程如下:
1,在頁(yè)面里添加必要的控件
2,創(chuàng)建返回值的Get屬性過(guò)程
3,創(chuàng)建可以返回表單的按鈕和鏈接按鈕
4,在按鈕單擊事件處理程序中調(diào)用Server.Transfer方法轉(zhuǎn)移到指定的頁(yè)面
5,在第二個(gè)頁(yè)面中,我們就可以使用Context.Handler屬性來(lái)獲得前一個(gè)頁(yè)面實(shí)例對(duì)象的引用,通過(guò)它,就可以使用存取前一個(gè)頁(yè)面的控件的值了
以下代碼綜合實(shí)現(xiàn)上述步驟過(guò)程的代碼:
  源頁(yè)面WebForm1.aspx.cs中的部分代碼:
    把以下的代碼添加到頁(yè)面中
public string Name
{
     get
     {
         return TextBox1.Text;
     }
}

public string EMail
{
     get
     {
         return TextBox2.Text;
     }
}
  然后調(diào)用Server.Transfer方法
private void Button1_Click(object sender, System.EventArgs e)
{
     Server.Transfer("WebForm2.aspx");
}
   目標(biāo)頁(yè)面代碼:

在WebForm2.aspx中務(wù)必在第一句話添加

<%@ Reference Page="~/WebForm1.aspx" %>

<%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %>

然后在WebForm2.aspx.cs中添加如下。


private void Page_Load(object sender, System.EventArgs e)
{
     //create instance of source web form
     WebForm1 wf1;
     //get reference to current handler instance
     wf1=(WebForm1)Context.Handler;
     Label1.Text=wf1.Name;
     Label2.Text=wf1.EMail;
}

如果在調(diào)試的過(guò)程中遇到錯(cuò)誤.就到C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files中把新建的網(wǎng)站名的文件夾刪掉.(要先關(guān)掉Visual Studio開(kāi)發(fā)環(huán)境再刪)

4.使用@PreviousPageType指令

      這個(gè)指令是.net 2.0中的一個(gè)新指令,用于處理ASP.NET 2.0提供的跨頁(yè)面?zhèn)魉托鹿δ?用于批定跨頁(yè)面的傳送過(guò)程起始于哪個(gè)頁(yè)面.包含兩個(gè)屬性:

TypeName:設(shè)置回送時(shí)的派生類名

VirtualPath:設(shè)置回送時(shí)所傳送頁(yè)面的地址.

如下示例:

 

源頁(yè)面WebForm1.aspx中有一個(gè)TextBox,ID為txtName.在WebForm1.aspx.cs中設(shè)置一個(gè)屬性:

public TextBox Name

{

    get{return this.txtName;}//返回一個(gè)控件對(duì)象

}

 

在目標(biāo)頁(yè)面的設(shè)計(jì)文件中(WebForm2.aspx)的最上方加上:

 <%@ PreviousPageType VirtualPath="~/Page1.aspx"%>,

然后就能引用WebForm1.aspx中定義的屬性了.

在WebForm2.aspx.cs中可以有如下引用形式(假設(shè)WebForm2.aspx中有一個(gè)ID為lblName的Label):

 

lblName.Text="Hello"+PreviousPage.Name.Text+"<br />";

 5.利用某些控件的PostBackUrl屬性

示例:仍然是源頁(yè)面WebForm1.aspx和目標(biāo)頁(yè)面WebForm2.aspx.

WebForm1.aspx中的部分代碼:

<asp:Button ID="btnPostBack" Runat="server" Text="PBButton"></asp:Button>

<asp:TextBox ID="txtName" Runat="server" ></asp:TextBox>

<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>

 

WebForm2.aspx.cs中的部分代碼:

protected void Page_Load(object Sender,System.EventArgs e)

{

    TextBox txtName;

    Calendar calendar1;

 

    txtName=(TextBox)PreviousPage.FindControl("txtName");

    calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

    Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

}

 

使用這種方法存在一個(gè)問(wèn)題:如果在沒(méi)有單擊那個(gè)按鈕之前,也就是未處理WebForm1.aspx之前,有人請(qǐng)求了WebForm2.aspx,該怎么辦?這就需要在WebForm2.aspx中的代碼處理之前加一個(gè)判斷.使用IsCrossPagePostBack屬性,這與IsPostBack屬性很相似,它允許檢查請(qǐng)求是否來(lái)自WebForm1.aspx.如下:

protected void Page_Load(object Sender,System.EventArgs e)

{

    if(PreviousPage.IsCrossPagePostBack)

    {

        TextBox txtName;

        Calendar calendar1;

 

        txtName=(TextBox)PreviousPage.FindControl("txtName");

        calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

        Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

    }

    else

    {

        Response.Redirect("WebForm1.aspx");

    }

}

6.  使用Cookie對(duì)象變量
  這個(gè)也是大家常使用的方法,與Session一樣,是對(duì)每一個(gè)用戶而言的,但是有個(gè)本質(zhì)的區(qū)別,即Cookie是存放在客戶端的,而session是存放在服務(wù)器端的。而且Cookie的使用要配合ASP.NET內(nèi)置對(duì)象Request來(lái)使用。

a.aspx的C#代碼
private void Button1_Click(object sender, System.EventArgs e)
{
    HttpCookie cookie_name = new HttpCookie("name");
    cookie_name.Value = Label1.Text;
    Reponse.AppendCookie(cookie_name);
    Server.Transfer("b.aspx");
}

b.aspx中C#代碼
private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Request.Cookie["name"].Value.ToString();
}

7.  使用Application 對(duì)象變量
  Application對(duì)象的作用范圍是整個(gè)全局,也就是說(shuō)對(duì)所有用戶都有效。其常用的方法用Lock和UnLock。
a.aspx的C#代碼
private void Button1_Click(object sender, System.EventArgs e)
{
    Application["name"] = Label1.Text;
    Server.Transfer("b.aspx");
}

b.aspx中C#代碼
private void Page_Load(object sender, EventArgs e)
{
    string name;
    Application.Lock();
    name = Application["name"].ToString();
    Application.UnLock();
}
 

1.  使用QueryString變量

QueryString是一種非常簡(jiǎn)單的傳值方式,他可以將傳送的值顯示在瀏覽器的地址欄中。如果是傳遞一個(gè)或多個(gè)安全性要求不高或是結(jié)構(gòu)簡(jiǎn)單的數(shù)值時(shí),可以使用這個(gè)方法。但是對(duì)于傳遞數(shù)組或?qū)ο蟮脑挘筒荒苡眠@個(gè)方法了。下面是一個(gè)例子:

a.aspx的C#代碼

private void Button1_Click(object sender, System.EventArgs e)
{
    string s_url;
    s_url = "b.aspx?name=" + Label1.Text;
    Response.Redirect(s_url);
}

b.aspx中C#代碼
private void Page_Load(object sender, EventArgs e)
{
    Label2.Text = Request.QueryString["name"];
}

2.  使用Application 對(duì)象變量

Application對(duì)象的作用范圍是整個(gè)全局,也就是說(shuō)對(duì)所有用戶都有效。其常用的方法用Lock和UnLock。

a.aspx的C#代碼

private void Button1_Click(object sender, System.EventArgs e)
{
    Application["name"] = Label1.Text;
    Server.Transfer("b.aspx");
}

b.aspx中C#代碼

private void Page_Load(object sender, EventArgs e)
{
    string name;
    Application.Lock();
    name = Application["name"].ToString();
    Application.UnLock();
}

3.  使用Session變量

想必這個(gè)肯定是大家使用中最常見(jiàn)的用法了,其操作與Application類似,作用于用戶個(gè)人,所以,過(guò)量的存儲(chǔ)會(huì)導(dǎo)致服務(wù)器內(nèi)存資源的耗盡。

a.aspx的C#代碼

private void Button1_Click(object sender, System.EventArgs e)
{
    Session["name"] = Label.Text;
}

b.aspx中C#代碼

private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Session["name"].ToString();
}

4.  使用Cookie對(duì)象變量

這個(gè)也是大家常使用的方法,與Session一樣,其是什對(duì)每一個(gè)用戶而言的,但是有個(gè)本質(zhì)的區(qū)別,即Cookie是存放在客戶端的,而session是存放在服務(wù)器端的。而且Cookie的使用要配合ASP.NET內(nèi)置對(duì)象Request來(lái)使用。

a.aspx的C#代碼

private void Button1_Click(object sender, System.EventArgs e)
{
    HttpCookie cookie_name = new HttpCookie("name");
    cookie_name.Value = Label1.Text;
    Reponse.AppendCookie(cookie_name);
    Server.Transfer("b.aspx");
}

b.aspx中C#代碼

private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Request.Cookie["name"].Value.ToString();
}

5.  使用Server.Transfer方法

這個(gè)才可以說(shuō)是面象對(duì)象開(kāi)發(fā)所使用的方法,其使用Server.Transfer方法把流程從當(dāng)前頁(yè)面引導(dǎo)到另一個(gè)頁(yè)面中,新的頁(yè)面使用前一個(gè)頁(yè)面的應(yīng)答流,所以這個(gè)方法是完全面象對(duì)象的,簡(jiǎn)潔有效。

a.aspx的C#代碼

public string Name
{
    get{ return Label1.Text;}
}
private void Button1_Click(object sender, System.EventArgs e)
{
    Server.Transfer("b.aspx");
}

b.aspx中C#代碼
private void Page_Load(object sender, EventArgs e)
{
    a newWeb;   //實(shí)例a窗體
    newWeb = (source)Context.Handler;
    string name;
    name = newWeb.Name;
}

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ASP.NET頁(yè)面之間傳值的方法,我試過(guò)的有5種方法,現(xiàn)在總結(jié)如下:
.net如何實(shí)現(xiàn)頁(yè)面間的參數(shù)傳遞
asp.net WebForm頁(yè)面間傳值方法
ASP.NET學(xué)習(xí)筆記 ---系統(tǒng)對(duì)象與狀態(tài)管理大全
編寫global.asax
Asp.net頁(yè)面之間傳遞參數(shù)的幾種方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服