chobo
chobo

Reputation: 32301

Master page variables values being lost

I know this is pretty basic, but why do variables in master pages lose their value in the child pages?

For example if I have

[masterpage]

public string userId

... set userId in masterpage


[child page]

Master.userId // userId will always be empty? 

Upvotes: 0

Views: 1042

Answers (3)

Aykut Celik
Aykut Celik

Reputation: 511

i'd say try using Session["userId"]="..." and in otherpage.aspx =>Session[userId].ToString() will get the value

Upvotes: 1

Matthew Cox
Matthew Cox

Reputation: 13682

Because this is a stateless paradigm. If you want to maintain those values between post backs you have only a few options:

  1. Session State
  2. View State
  3. Database
  4. javascript / hidden values

Upvotes: 2

Alexander Galkin
Alexander Galkin

Reputation: 12554

Don't use master pages to store information about users -- it won't persist there after the re-load of your page.

If you need to store some user-specific information use the Session object to store it.

Here is an example.

Upvotes: 3

Related Questions