开发者

save a certain number into session on href in ASP.Net, C#

I already found this on this website.

how to pass session variable 开发者_JAVA技巧in href

But what I want is vice versa. and I can't get it done..

I need some correct syntax..

is it ......

..../home.aspx?<%Session["egSession"]=1%>

or

..../home.aspx?=<%Session["egSession"]=1%>

or

..../home.aspx?<%=Session["egSession"]=1%>

But i believe all of the above are wrong.. coz none of them are working..

Tkz..


Session.Add("egSession", 1) will add 1 to the session cookie egSession.

You could also probably be sure it doesn't already exist by doing so:

Session.Remove("egSession");
Session.Add("egSession", 1);

To get the querystring value from the address you would (code behind do)

var value = Request["egSession"];

So that means you could do:

Session.Remove("egSession");
Session.Add("egSession", Request["egSession"]);

Hope that helps!

** UPDATE **

If you can't touch the .cs files, you can do this in the ASPX-file, by wrapping your code in <% ... code goes here ... %>


if the new session variable value is know, e.g. 1 in your sample setting it can be done anywhere

<% Session["egSession"]=1; %>

if you want to pass it through as a query parameter do this:

..../home.aspx?egSession=<%=Session["egSession"]%>

The point is, you need a name for the value, i.e. egSession but you may call it what ever you want.

However, if you alldready know the value you can simply do:

..../home.aspx?egSession=1


From what I am understanding you want something like this:

APage

<a href="..../home.aspx?egSession=1">Take Me home</a>

Home.aspx.cs: the code behind page, in say the OnPageLoad Event

Session["egSession"] = Request.QueryString["egSession"];

Home.aspx

<div>Session: <% =Session["egSession"] %></div>
<div>Query String: <% = Request.QueryString["egSession"] %></div>

If you are trying to to it all in one I would try the following:

APage.aspx.cs

Create a public method (change the type of the input parameter if needed)

public string SessionMagic(object input)
{
     Session["egSession"] = input;
     return Session["egSession"].ToString();
}

APage.aspx

<a href="..../home.aspx?egSession=<%= SessionMagic(1)%>">A Link</a>

*UPDATE: *

If you can not update the .cs files you can add server side code in the aspx page, not great practice but it can be done. Encapsulate the code in script tags with the run a server attribute set. E.g.:

<script runat="server">
  public string ServerSideFunction(string input)
  {
     Session["egSession"] = Request.QueryString["egSession"];

     public string SessionMagic(object input)
     {
         Session["egSession"] = input;
         return Session["egSession"].ToString();
     }

  }
</script>


you can use javascript with cookie to store the value that you want to set into Session.

eg:- url- url.apsx/egValue=1 read this url using javascript. Put the key and value into cookie. Then you can read cookie value from server and put it into session.

hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜