views:

964

answers:

5

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.

A: 

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
Thanks Daniel for the reply.Can you tell me how to handle hidden fields in a code behind page?
archana roy
Without a form you cannot have a hidden form value.
Daniel Elliott
+1  A: 

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
Do we need to declare "MyValue" explicitely?
archana roy
"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
+1  A: 

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.

Scozzard
Thanks for the reply. Can you tell me how to handle hidden fields in a code behind page?I dont want to use session.
archana roy
@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
Are you suggesting me to use this--HttpContext.Current.Items["var"]=value;var value=HttpContext.Current.Items;
archana roy
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
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.
archana roy
I have added a link to an msdn overview of session state which is a good place to start.
Scozzard
A: 

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
Thanks for the reply.Can you tell me how to handle hidden fields in a code behind page?
archana roy
here http://www.csharphelp.com/archives/archive184.html is an article about using hidden fields
Beatles1692
A: 

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.

nickyt