2012-03-21 53 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,应用程序变量和数据库来保存您的数据。