2012-03-21 51 views
2

我正在ASP.NET c#應用程序中工作。 我來到了一個部分,我需要保留一些值後,response.redirect相同的頁面沒有使用額外的QueryString或會話,因爲會話或多或少可能會負擔服務器的性能,即使只是一個小值。ASP.NET C# - 如何在Response.Redirect後保留價值

下面是我的代碼片段:

protected void ddl_SelectedIndexChanged(object sender, EventArgs e) 
{ 
string id = ddl.SelectedValue; 
string id2 = ddl2.SelectedValue; 
Response.Redirect("http://sharepoint2007/sites/home/Lists/CustomList/DispForm.aspx?ID=" + id); 
} 

我想保留的Response.Redirect後的值ID2,我已經試過ViewState的,但看起來像重定向後,它把網頁作爲新的一頁, ViewState值消失了。

更新:

我打算保留值重定向要在下拉列表中選擇值綁定回後。

請幫忙。

謝謝您的高級。

回答

3

使用cookie將這樣的伎倆:

protected void ddl_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string id = ddl.SelectedValue; 
    string id2 = ddl2.SelectedValue; 
    HttpCookie cookie = new HttpCookie("SecondId", id2); 
    Response.Cookies.Add(cookie); 
    Response.Redirect("http://sharepoint2007/sites/home/Lists/CustomList/DispForm.aspx?ID=" + id); 
} 

protected void OnLoad(object sender, EventArgs e) 
{ 
    string id2 = Request.Cookies["SecondId"]; 
    //send a cookie with an expiration date in the past so the browser deletes the other one 
    //you don't want the cookie appearing multiple times on your server 
    HttpCookie clearCookie = new HttpCookie("SecondId", null); 
    clearCookie.Expires = DateTime.Now.AddDays(-1); 
    Response.Cookies.Add(clearCookie); 
} 
+0

糾正我,如果我錯了,我雖然餅乾被存儲在用戶的瀏覽器?我儘量不要存儲值到服務器的原因有用戶訪問的頁面的千,這可以鼓勵的性能問題。 – sams5817 2012-03-21 07:36:26

+0

是,該值存儲在客戶端上。在這種情況下,您不會遇到性能問題。它的規模很好。您還應該知道,您可以在會話中存儲簡單的ID,而不會產生嚴重的性能/可伸縮性問題。 – linkerro 2012-03-21 07:44:31

+0

只是不知道是使用cookie的缺點?如任何瀏覽器設置可能會阻止在cookie中存儲數據? – sams5817 2012-03-21 07:50:34

3

。利用會議變量會爲你

代碼爲你

Session["id2"] = ddl2.SelectedValue; 

因爲你是從一個頁面重定向到另一個頁面視圖狀態是不會幫你,會話varialbe能能夠存儲值直到用戶註銷網站或直到會話結束時,ViewState在您自動回覆頁面時有幫助

如果可能的話,你可以在查詢字符串連接ID2變量只有當你與ID1變量做

+0

Pranay蛙,感謝您的回覆。我的確提到,由於有些擔心,我並不想用會話 – sams5817 2012-03-21 07:21:14

+0

@ sams5817 - 比任何去餅乾或使用查詢字符串 – 2012-03-21 07:26:43

+0

指定-1 reasonf的?????????????? ????????? – 2012-03-21 19:45:06

1

可以使用會話查詢字符串

實現它通過會議

在您的首頁:

Session["abc"] = ddlitem; 

然後使用你的下一個頁面訪問會話:

protected void Page_Load(object sender, EventArgs e) 
{ 
    String cart= String.Empty; 
    if(!String.IsNullOrEmpty(Session["abc"].ToString())) 
    { 
     xyz= Session["abc"].ToString(); 
     // do Something(); 
    } 
    else 
    { 
     // do Something(); 
    } 
} 

-

通過查詢字符串

在第一頁:

private void button1_Click(object sender, EventArgs e) 
{ 
    String abc= "ddlitem"; 
    Response.Redirect("Checkout.aspx?ddlitemonnextpage" + abc) 
} 

在你的第二頁:

protected void Page_Load(object sender, EventArgs e) 
{ 
    string xyz= Request.QueryString["ddlitemonnextpage"].ToString(); 
    // do Something(); 
} 
2

除了會話,查詢字符串,您還可以使用cookie,應用程序變量和數據庫來保存您的數據。