views:

87

answers:

4

Here is my master page code behind:

namespace mysite.MasterPages
{
    public partial class Main : System.Web.UI.MasterPage
    {
        public bool isLoggedIn;

        protected void Page_Load(object sender, EventArgs e)
        {
            isLoggedIn = Request.IsAuthenticated;      // Is the user currently logged in
        }
    }
}

Here is my register page code behind:

namespace mysite
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((MasterPage)Page.Master).isLoggedIn)
            {
                Response.Redirect("default.aspx");
            }
        }
    }
}

I'm trying to make the isloggedIn accessible to all pages using that as a master page! I get errors like:

Error 2 The name 'isLoggedIn' does not exist in the current context

Error 3 'System.Web.UI.MasterPage' does not contain a definition for 'isLoggedIn' and no extension method 'isLoggedIn' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?)

Any help appreciated.

A: 

Usually one would store such Global 'states' inside a session variable. Are passing it around as a query string parameter?

And why are you not putting the code in the code behind?

Edit 1:

Just move this logic:

  Response.Redirect("default.aspx");

directly into your masterpage:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)      // Is the user currently logged in
        {
            Response.Redirect("default.aspx");
        }
    }

If this is the default behaviour you want for all pages that will be using this Master Page.

Edit 2:

you said in a comment :

and how would i go about accessing the variable on other pages?

There are 3 ways I can think of:

  • Query String (Per Page Request)
  • ViewState (Per Page)
  • Session Variable (Available Globally)
Darknight
It's from the ASP membership system not query string.
Tom Gullen
That's fair enough, but why are you not using the code behind? I suspect your classic ASP background may play a part.
Darknight
Yes, I'm just used to having it all on one page! I still don't quite understand the code behind so will read up on it.
Tom Gullen
It's better to configure authentication and authorization in Web.config rather than do manual check on pages
abatishchev
What?! why the downvote?! abatishchev metions using the viewstate which I've already mentioned yet he is upvoted?!
Darknight
A: 

The problem here is that your are declaring isLoggedIn within an inline code-block, so it will only be scoped to within that block.

You'll need to add it as a variable within the code-behind class or within <script runat="server"></script> tags, if adding the server-side code inline, i.e. (trimmed down for brevity):

<script runat="server">

    public bool IsLoggedIn;

    protected void Page_Load(object sender, EventArgs e)
    {
        IsLoggedIn = Request.IsAuthenticated;
    }

</script>

You could then access the master page in subpages like so:

<%
    if (((MasterPage)Page.Master).IsLoggedIn)
    {
    }
%>

However, I'm not sure that this is the best way to achieve what you want. I would probably take that logic out of the master page and stick it in a purpose built authentication class or service.

nukefusion
Thanks, but the variable is logged in doesn't seem to be accessible to the pages that use that master page which is the problem.
Tom Gullen
Very true, I've updated my answer.
nukefusion
Thanks, it didn't seem to work still, I've changed the question because everyone keeps tellingme to use code behind
Tom Gullen
Only Content controls are allowed directly in a content page that contains Content controls.
Tom Gullen
Don't write code in markup. This is VERY out-of-date style from .NET 1.x. Only if you use ASP.NET MVC and different helpers
abatishchev
+2  A: 

add <%@ MasterType VirtualPath="~/Main.master" %> to your page markup.

and your this.Master's type becomes AlphaPack.MasterPages.Main instead of System.Web.UI.MasterPage. So you will be able to access it without cast:

 this.Master.IsLoggednIn

Currently you need do next:

((AlphaPack.MasterPages.Main)this.Master).isLoggednIn

And better - create a property. And hold data not in variable but in ViewState (read Control State vs. View State):

namespace AlphaPack.MasterPages
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.IsLoggedIn = Request.IsAuthenticated;
        }

        public bool IsLoggedIn
        {
            get { return this.ViewState["isLoggedIn"] as bool? ?? false; }
            set { this.ViewState["isLoggedIn"] = value; }
        }
    }
}

And what about code-behind. I recommend to use Web App project, not Web Site project (which is out-of-date)!

Next markup syntax is used. Web app:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyNamespace.MyPage" MasterPageFile="~/MyMaster.master" Title="MyTitile" %>

and web site:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" MasterPageFile="~/MyMaster.master" Title="MyTitile" %>
abatishchev
Thanks, and how would i go about accessing the variable on other pages? I keep getting errors. (Just so you know you missed a })
Tom Gullen
@Tom: Thanks, fixed. See my updated post - second paragraph: use `this.Master.IsLoggednIn` on every page where you put `MasterType`. See more at http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx
abatishchev
+1  A: 
if (((MasterPage)Page.Master).isLoggedIn)

Should be

if (((mysite.MasterPages.Main)Page.Master).isLoggedIn)

You cast Master to a MasterPage type (which is useless as it is already a MasterPage). But MasterPage doesn't contain the property isLoggedIn. That's why you can't access it.

So, just cast the Master property to the right type, in your case mysite.MasterPages.Main

Julien N