0

I have an ASP.NET webpage written in C#. In my master page, I have a <select> element which allows the user to switch between two different databases. I thought I could just add code to the Master Page Page_Load() method to check if the form containing the select has been submitted, and if so, set some session variables which define what database I'm connecting to.

The problem is, it looks like the Page itself loads before the Master page's Page_Load method because when I submit the form it does update my session variable, but my queries are still looking at the original database. However, if I submit the form twice, it registers.

I'm guessing that the Master Page's Page_Load method MUST fire AFTER the Page's Page_Load method. Is there somewhere I can place my code so it will be triggered on ALL pages but will execute BEFORE the page loads?

All I'm doing is...

if (Request.Form["database"] != null) {
     Session["database"] = Request.Form["database"];
}
2
  • 2
    You are correct; the master page load fires after the content page. See this answer for a reference of the lifecycle. Commented Feb 18, 2016 at 14:38
  • @codechurn thank you so much! Your link lead me to the Master page's Init() method. I placed my code there and all is working correctly now. If you want to add your comment as answer I will accept as correct. Thanks again! Commented Feb 18, 2016 at 14:40

3 Answers 3

2

In ASP.NET WebForms, Page events are always processed from the inner most level out. This means that the event will always fire on the Content page first and will then proceed in to the master page.

One tactic you can employ is to hook into an event earlier in the page lifecycle, which will fire on the Master page before it does on the content page.

See the answer in this post for a reference of the page life cycle.

Sign up to request clarification or add additional context in comments.

Comments

2

You got a reference to your masterpage from your page. You can call findcontrol to get the select.

Master.FindControl("IDofYourSelect");

To get this working you should use a asp:dropdownlist instead of a select!

Now you can handle the connection selection inside your page-code just before you need the db-connection

3 Comments

just curious why should I use a asp:dropdownlist rather than a select?
to find the control inside your page! then you can handle the db selection inside your page-code just before you need a connection
thank you for your response. For my purposes, using the Master Page's Page_Init() method worked perfectly. I do appreciate your answer though, thank you.
1

This can be accomplished by building your own IHttpModule implementation and registering it in your web.config file.

https://msdn.microsoft.com/en-us/library/ff649096.aspx

public class DatabaseSwitchModule : IHttpModule
{
    private HttpApplication httpApp;
    public void Init(HttpApplication httpApp)
    {
        this.httpApp = httpApp;
        httpApp.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
    }

    public void OnPreRequestHandlerExecute(object sender, EventArgs e)
    {
        NameValueCollection form = httpApp.Request.Form;
        if (form["database"] != null)
        {
            httpApp.Session["database"] = form["database"];
        }
    }
}

This way you make sure that the session gets treated correctly, even if you were to change to another Master Page in the future (or use different ones for different pages).

You really shouldn't trust a master page to perform important business logic. Pages and master pages are for display.

2 Comments

thank you for your response. For my purposes, using the Master Page's Page_Init() method worked perfectly. I do appreciate your answer though, thank you.
Come back when you want nested master pages, or different master pages for different parts of your site, and don't want to duplicate your code. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.