Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4732
  • Last Modified:

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
0
kashwmu
Asked:
kashwmu
  • 6
  • 5
  • 5
2 Solutions
 
David RobitailleAnalyst ProgrammerCommented:
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
0
 
abelCommented:
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.
0
 
abelCommented:
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.
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
David RobitailleAnalyst ProgrammerCommented:
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.
0
 
abelCommented:
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 --
0
 
kashwmuAuthor Commented:
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
0
 
David RobitailleAnalyst ProgrammerCommented:
"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.
0
 
kashwmuAuthor Commented:
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
0
 
David RobitailleAnalyst ProgrammerCommented:
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.
0
 
abelCommented:
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
0
 
kashwmuAuthor Commented:
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
0
 
David RobitailleAnalyst ProgrammerCommented:
hum....
The plan was to use the ViewState to "hold" the value across pages using a control on a master page. The problem, has i discovered after my second post, is that I got information that a master page is not a page itself. So, if it`s true, ViewState cannot be used so share a value across pages.
You could continue on that trail, but, if you make that masterpage property work, I doubt the it will be useful because it will be reinitialized between each pages..
0
 
abelCommented:
> 2. I added <%@ MasterType VirtualPath="~/MasterPage.master" %> to Report.aspx.
does your path resolve? I.e., is the master in the root dir? Is your MasterPageFile set for that page?

What is the version of your .NET? Might it be that on .NET 2.0 (if you have that) it doesn't work? I have 3.5, though I do remember that I used this technique before I moved over.

As you notice, I'm a bit running out of ideas. But, as davrob60 excellently points out: the viewstate is a per-page state, not a per-session state, and if you use the viewstate from the master, that's fine, but it will be unavailable across pages. I just tested it to be sure, and it is correct, unfortunately.

Still, using a masterpage to hold a property that accesses a generic value that you keep inside a session is very good practice, because you make it accessible for anybody using that masterpage.

-- Abel --
0
 
kashwmuAuthor Commented:
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.
0
 
kashwmuAuthor Commented:
My problem was solved but the final few messages did not get resloved for me.
0
 
abelCommented:
> 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 --
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now