Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

BasePage properties sometimes there, sometimes not.

Posted on 2013-01-30
6
Medium Priority
?
208 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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