Solved

BasePage properties sometimes there, sometimes not.

Posted on 2013-01-30
6
202 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
ID: 38834842
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
ID: 38835997
@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
ID: 38839089
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Expert Comment

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

Author Comment

by:SeanNij
ID: 38839130
@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
ID: 38854270
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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