Solved

BasePage properties sometimes there, sometimes not.

Posted on 2013-01-30
6
195 Views
Last Modified: 2013-02-05
Hi All,

I have been searching like crazy but can't seem to get the answer I want.

I have a ASP.NET 2.0 project.
I inherit a BasePage called DefaultBasePage to check whether user is logged in.
Also uses properties in this page to check on my master page if I should display the menu or not.
I've got 2 pages here that are exactly the same with regards to this, but one of the pages gets the value from the base page and the other doesn't.

Here is my DefaultBasePage.cs

public class DefaultBasePage : System.Web.UI.Page
{
    private bool bShowHeadersFooters;
    private bool bNeedsToBeLoggedIn = true;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if (Session["ID"] == null
            && !Request.Url.ToString().Contains("login.aspx")
            && bNeedsToBeLoggedIn)
        {
            Response.Redirect(Application["URL"] + "login.aspx?ReturnUrl=" + Server.UrlEncode(Request.Url.ToString()));
        }
    }
    public bool ShowHeadersAndFooters
    {
        get { return bShowHeadersFooters; }
        set { bShowHeadersFooters = value; }
    }

}

Open in new window


DefaultMaster.master.cs

        if (Page is DefaultBasePage)
        {
            bShowHeadersAndFooters = ((DefaultBasePage)Page).ShowHeadersAndFooters;
            if (Session["ID"] != null)
            {
                if (bShowHeadersAndFooters)
                {
// Show Menu. Works sometimes and sometimes not.
}
}
}

Open in new window


Any ideas would be greatly appreciated as I am banging my head against the wall because of this,

Thanks in advance,
S
0
Comment
Question by:SeanNij
  • 3
  • 2
6 Comments
 
LVL 10

Assisted Solution

by:Eduard Ghergu
Eduard Ghergu earned 500 total points
Comment Utility
Hi,

You're neglecting the stateless nature of the web. Try something like:

public bool ShowHeadersAndFooters
    {
        get { return (bool)Session["ShowHeadersFooters"]; }
        set { Session["ShowHeadersFooters"] = value; }
    }
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
@seanNij,

Where are you setting your ShowHeadersAndFooters properties? By default it's false.
You must set them somewhere on your page.
0
 
LVL 1

Accepted Solution

by:
SeanNij earned 0 total points
Comment Utility
Hi,

Thanks for the reply.

It did not work, however you did give me an idea, ghergu.

So what I've done is this:

In my aspx.cs file I did:

    protected void Page_Init(object sender, EventArgs e)
    {
        Session["ShowHeadersAndFooters"] = false;
    }

Open in new window


and in my master.cs I did:

    protected void Page_Load(object sender, EventArgs e)
    {
            if (Session["ShowHeadersAndFooters"] != null)
                bShowHeadersAndFooters = (bool)Session["ShowHeadersAndFooters"];
    }

Open in new window


Works like a charm!
Thanks for the idea!
S
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:Eduard Ghergu
Comment Utility
Hi!
You're welcome! Any "reward" for this?
0
 
LVL 1

Author Comment

by:SeanNij
Comment Utility
@ghergu Close Request Pending. Points awarded to you. Will close auto on 02/04/2013 if no objections.
0
 
LVL 1

Author Closing Comment

by:SeanNij
Comment Utility
First answer gave me an idea and created my own solution. Point still awarded to the person who gave me the idea.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now