Question

I declared in master page load:

Session["sessionString"] = "stringX";

in my web page on load also, i call it

string sessionString= (string)Session["sessionString"];

i debug and value in web page is "" what is wrong?

Was it helpful?

Solution 2

JW Lim is correct...content page's load event will fire before Master page load.

@user3445314 , you can use "Session_Start" event in Global.asax and set values same as you set in master page.

"Session_Start" event fires first and then your content page load event occur.

I hope this may be solve your problem

OTHER TIPS

The Page_Load event of a content page is called before the Page_Load event of the master page (see this SO answer and MSDN article). Hence, when you call

string sessionString= (string)Session["sessionString"];

in your web page, Session["sessionString"] does not contain any value yet and defaults to an empty string.

As a workaround, you could set the value of Session["sessionString"] on the Init or PreLoad events of your master page.

Declare Session in Master PreRender

protected override void OnPreRender(EventArgs e)
{
   Session["sessionString"] = "stringX";
}

And get it by ContetPage OnUnload event

protected override void OnUnload(EventArgs e)
{
   string sessionString= (string)Session["sessionString"];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top