Link to home
Start Free TrialLog in
Avatar of kashwmu
kashwmu

asked on

Using Master Pages for Global Variables

Hi All,
What i am trying to do is set a field that i have on the master page to hold a users login id so that it can be stored across multiple pages but this does not seem to work. I am far from and expert at ASP.NET but i am not changing the value just setting it and since i am using the same master page for all sub pages i believed this to work. What i am trying to accomplish is just a Global Variable like a windows form app. Session variables do not work because we are on a web farm and the computers are set not to use cookies and i really dont like to dirty the querystring everytime i go from page to page. Does anyone have a suggestion. Thanks
Avatar of David Robitaille
David Robitaille
Flag of Canada image

i would not use  Master Pages for Global Variables.
but do you know you could store session state on  SQL server???
http://support.microsoft.com/kb/317604
A global variable is not a good idea for this, because that will be really global (i.e., visible to every page on any request) and one variable can only hold one value.

The thing you are looking for is Sessin. If you are on C# and I think you are, you can set the session like this:

Session["userid"] = myUserId;
and you can retrieve it like this:

int earlierStoredUserId = 0;
int.TryParse(Session["userid"], out earlierStoredUserId);
which will store it in the variable earlierStoredUserId, which will contain zero if there wasn't a userid to begin with (i.e. on the first visit of a new user to your page).

This session is available to every page and every masterpage and every control. It is meant for this kind of storage and will be unique per user, but can be retrieved at any time.
oops, I didn't see that last sentence... you don't want to use sessions... But if you are on a web farm, I'm sure you use a state server or, like davrob60 suggests, sql server for it. Both are quite easy to setup.
If you really what to use store a global variable on the masterpage, you could use a hiddencontrol to store the values. persistence of the values will be assumed by the ViewState mecanism.
Agreed, and you can use a public readonly property on the masterpage

public int UserId
{
    get
    {
        return int.Parse(myHiddenField.Value);
    }
}
and on the page you can do the followoing:

<%@ MasterType VirtualPath="~/masterpage.master" %>
in your page ASPX and then in your code behind file, you can use the following to access your masterpage data:

int myUserIdVariableOnThePage = Master.UserId;
which will effectively be the way I would go about implementing the proposal of davrob60 in the last post, which fits your original request to use the masterpage for storing data (ViewState).

-- Abel --
Avatar of kashwmu
kashwmu

ASKER

Ok, you guys are going in the right direction and i appreciate that i just need a little more help it looks like.
I added the <%@ MasterType VirtualPath="~/masterpage.master" %> directive to the page that i am trying to pull the data from and i was not able to see the public string that i declared. There was no Master just the normal MasterPage that is standard. What i was tryign to do before was i created two labels on the master page and then i was loading them with data during my initial load of the program using a FindControl(). Then i could set it to a value but that value would never persist accross multiple pages. This was a mystery to me thus the reason i asked the question. I have no problem with the public property idea i just did what you posted and was not able to see the property. Any help is appreciated. Thanks
"but that value would never persist across multiple pages."
That what I was worrying about. I searshed for any reference about viewstate and masterpage and I found nothing revalant, (at first). but i just fond something.
the problem is that ViewState is being saved/restored across same page round trips. The problem is that a master page is not a page itself and that why never persist across multiple pages.
http://forums.asp.net/p/1257612/2341989.aspx#2341989 
http://forums.asp.net/p/1127427/1784044.aspx#1784044
so I think you must rely on the session option.
Avatar of kashwmu

ASKER

Using a session might be the correct option and i will have to get the people to set that up but i was wondering if anyone has any info on the master page using a public property. I have found many pages that say all you have to do is add the <%MasterType%> directive and they you can access this by just using this.Master but that is not true for me. Can anyone help with that like was stated by ABEL in an earlier comment. Thanks
The "MasterType" thing is just something to "interface" with the masterpage. It used, if you have something you need to access form the content page.
It could be use, as an example, to update a title or read the content of a textbox, but i dont think it could be applied to your case.
Apparently there are some questions about my approach. Just to be sure that we're on the same page, I tested my approach step by step. Here's what I did:

  1. Add a master page to my existing project with the name MyMaster.master
  2. Add the following to the master page code-behind (see code section)
  3. Add a page to my project called mypage.aspx
  4. Add the following to that page: <%@ MasterType VirtualPath="~/MyMaster.Master" %>
  5. Went to the code behind of the mypage.aspx page and type "Master.Us" inside a method. The result is in the screenshot:
If you do that too, what error do you get or, more precisely, what happens?

 If you have this example working, you can make use of the viewstate through a backdoor by using a hidden field with a runat=server, you can even set its Visible=False to prevent it being rendered into the HTML and you can use its value the way you would use any value that you want to persist with a page.

-- Abel --

namespace WebApplication1
{
    public partial class MyMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        public int UserTest = 30;
        public int UserId
        {
            get
            {
                return 20;
            }
        }
    }
}

Open in new window

ScreenShot243.png
Avatar of kashwmu

ASKER

Your example seems easy to me i just can't get it to work. When i type Master.* it does not bring up the property that i am trying to use. I am really not sure what i am doing different from you.

1. I have 2 pages, MasterPage.Master and Report.aspx
2. I added <%@ MasterType VirtualPath="~/MasterPage.master" %> to Report.aspx.
3. The below code is the MasterPage.Master code.
4. The screen shot is what i see when i type Master.Lo into the code. I do not see the property that i am trying to find.

Sorry about all of this hassle i just don't understand why it does not work.

Thanks for all of the assistance.
//Master Page Code
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
 
    public int LoginID
    {
        get { return 20; }
    } 
 
}

Open in new window

screenshot.bmp
ASKER CERTIFIED SOLUTION
Avatar of David Robitaille
David Robitaille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kashwmu

ASKER

It could be a .NET 3.5 thing but as i read around it looks like it can be acomplished on 2.0 but i believe that we all have spent enough time on it and since i can not get it to work i am going to just use a state server for sessions. I appreciate all of the help and since i am new at this i will try and give you both some points. I appoligise if i screw something up.
Avatar of kashwmu

ASKER

My problem was solved but the final few messages did not get resloved for me.
> I appoligise if i screw something up.

no, you didn't screw anything up. welcome to EE! ;-)
> My problem was solved but the final few messages did not get resloved for me.

I would, however, be intrigued to here of a solution if you find out what's up, because this is one of the more common scenario's with .NET and I feel like overlooking something. So, you're on .NET 2.0, but you're right, it can be done. This story tells what's going on behind the scenes, maybe you want to check if those methods are actually created for you: http://odetocode.com/Blogs/scott/archive/2005/07/16/1944.aspx (and if not, check whether you have the latest bug-fixes downloaded from Microsoft and/or use the CType/DirectCast workaround that is laid out on that very same page)

-- Abel --