How to pass a hidden field from one page to another?

前端 未结 5 2002
别那么骄傲
别那么骄傲 2021-01-28 01:23

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 p

相关标签:
5条回答
  • 2021-01-28 01:41

    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();
    
    0 讨论(0)
  • 2021-01-28 01:44

    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

    0 讨论(0)
  • 2021-01-28 01:49

    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.

    0 讨论(0)
  • 2021-01-28 01:53

    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.

    0 讨论(0)
  • 2021-01-28 01:57

    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;
    
    0 讨论(0)
提交回复
热议问题