0

I have a .Net class library. I want to pass a hidden variable from once code behind page and fetch it in another code behind page. PLease note I dont have any design page(aspx page) where I can use form tag and get/post method. How can we do this?

NB: I want to use hidden fields to pass value from 1 page to another.

5 Answers5

1

you can save the value in the Session as such:

Session["YourName"] = yourvalue;

in code behind you do this:

Session["MyValue"] = "theValueYouWantToPass";

in your other page you do:

string valueFromAnotherPage = Session["MyValue"].ToString();
roman m
  • 26,012
  • 31
  • 101
  • 133
  • Do we need to declare "MyValue" explicitely? –  Sep 22 '09 at 07:05
  • "MyValue" is just a name for your session variable ... you can call it whatever you want. once you assign that actual value to "MyValue" variable in session, you can always retrieve it like so: string valueFromAnotherPage = Session["MyValue"].ToString(); – roman m Sep 22 '09 at 16:40
1

If you are wanting to keep the variable hidden then you could use a session to store your object.

E.g.,

Setting the session value

Session["HiddenValue"] = "something";

Getting the session value

string something =  (string)Session["HiddenValue"];

Do keep in mind however, that sessions are only for a limited time (this can be configured thorugh IIS and your web configuration).

Here is a good resource to learn about sessions and session state.

Scott
  • 1,208
  • 9
  • 28
  • Thanks for the reply. Can you tell me how to handle hidden fields in a code behind page?I dont want to use session. –  Sep 22 '09 at 06:41
  • 1
    @archana roy: every time a browser requests a different page, you lose all the variable values you had on the current page, unless you save them in session on in HTTPContext – roman m Sep 22 '09 at 06:44
  • Are you suggesting me to use this-- HttpContext.Current.Items["var"]=value;var value=HttpContext.Current.Items; –  Sep 22 '09 at 06:45
  • i suggested to use the Sessin, @Beatles1692 suggested the HttpContext ... i just don't understand why don't you want to use the session? – roman m Sep 22 '09 at 06:47
  • I dont have much knowledge on session. Can you please let me know about using Session in code behind pages in bit more detail? Thanks a lot. –  Sep 22 '09 at 06:54
  • I have added a link to an msdn overview of session state which is a good place to start. – Scott Sep 22 '09 at 07:07
0

You could use a Session variable? They are not my preference but it would satisfy your need.

Session["VariableName"] = something;

object somethingOnNextPage = Session["VariableName"];

Kindness,

Dan

Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
0

You can use Session or HttpContext.Current.Items .If you value is a temporary variable I suggest using HttpContext.Current.Item instead of session since it will be gone as soon as current HttpContext is gone but items that are stored in Session won't be cleared until session is expired.

Session["var"]=value;

var value=Session["var"];

HttpContext.Current.Items["var"]=value;

var value=HttpContext.Current.Items;
Beatles1692
  • 5,214
  • 34
  • 65
  • Thanks for the reply. Can you tell me how to handle hidden fields in a code behind page? –  Sep 22 '09 at 06:39
  • here http://www.csharphelp.com/archives/archive184.html is an article about using hidden fields – Beatles1692 Sep 22 '09 at 07:04
0

What about cross-page postbacks (see Cross-Page Posting in ASP.NET Web Pages, http://msdn.microsoft.com/en-us/library/ms178139.aspx)? Never really used it, but it could be an option. Otherwise, you could access your hidden form element via the old school Request.Form . Another option could be to always have this hidden element on every page by putting it in the master page. Then you expose it as a public property and can get/set it to your heart's content.

nickytonline
  • 6,855
  • 6
  • 42
  • 76