简体   繁体   中英

ASP.NET C# Check for FORM value in Master Page

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"];
}

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM