標籤

2013年3月9日 星期六

ASP.NET跨頁面傳值技巧總結

1. 使用QueryString變量
QueryString是一種非常簡單的傳值方式,他可以將傳送的值顯示在瀏覽器的地址欄中。如果是傳遞一個或多個安全性要求不高或是結構簡單的數值時,可以使用這個方法。但是對于傳遞數組或對象的話,就不能用這個方法了。下面是一個例子:

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 對象變量
Application對象的作用範圍是整個全局,也就是說對所有用戶都有效。其常用的方法用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變量
想必這個肯定是大家使用中最常見的用法了,其操作與Application類似,作用于用戶個人,所以,過量的存儲會導致服務器內存資源的耗盡。

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();
}

最後用這個成功了!

.NET 清除 Session 
1. clear() 清空所有 key 值
2. removeAll() 即呼叫 clear()
3. remove("Key") 刪除某 Key 的值
4. Abandon() 清除所有 Key 值,並呼叫 Session_End


4. 使用Cookie對象變量
這個也是大家常使用的方法,與Session一樣,其是什對每一個用戶而言的,但是有個本質的區別,即Cookie是存放在客戶端的,而session是存放在服務器端的。而且Cookie的使用要配合ASP.NET內置對象Request來使用。

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();
}

您無法直接刪除使用者電腦上的 Cookie。然而,您可以將 Cookie 的到期日設定為過去的日期,引導使用者的瀏覽器刪除 Cookie
判斷 Cookie 是否存在,如果是的話,請使用相同名稱建立新的 Cookie。
將 Cookie 的到期日設定為過去的時間。
將 Cookie 加入 Cookies 集合物件。
下列程式碼範例,示範了如何在 Cookie 上設定過去的到期日。
If (Not Request.Cookies("UserPreferences1") Is Nothing) Then
Dim myCookie As HttpCookie
myCookie = New HttpCookie("UserPreferences1") 
myCookie.Expires = DateTime.Now.AddDays(-1D)
Response.Cookies.Add(myCookie) 
End If


5. 使用Server.Transfer方法

這個才可以說是面象對象開發所使用的方法,其使用Server.Transfer方法把流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以這個方法是完全面象對象的,簡潔有效。

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; //實例a窗體
newWeb = (source)Context.Handler;
string name;
name = newWeb.Name;
}

6.Cross-Page Posting
定位至于源網頁位于相同的應用程序的網頁,可以讀取源網頁的值和公共屬性,但是不要按瀏覽器中的重新整理和上一步,這樣會照成無法預期的效果
eg:
aspx
您的名字:
cs:
TextBox name = (TextBox)PreviousPage.FindControl("name");//非public 成員
Label1.Text = "歡迎你:" + name.Text;

previouspage本身屬于page類,并且只有在來源網頁和目標網頁屬于同一個asp.net應用程序下的時候才能引用,如果網頁不是跨網頁公布的目標,或者是網頁在不同的應用程序之中,就不會初始化previouspage屬性,就不能使用previouspage來存取任何信息

獲得網頁的公共屬性:

source.cs:
public string UserName
{
get{return aa.text;}
}

cross-page posting.aspx:
cross-page posting.cs:
lab.text = previousopage.username;
//一個網頁中只能指示一個previousPage,不然出出現編譯錯誤

使用 DiskSpd 測試磁碟效能

  DiskSpd 是微軟創建的命令行磁碟測試工具。它結合了強大的IO工作負載定義來測量磁碟效能。由於它支援自由配置和調整參數,使其成為存儲效能測試、驗證和基準測試的理想工具。 步驟 1. 從 GitHub (說明) https://github.com/Microsoft/di...