简体   繁体   English

ASP.NET C#检查母版页中的FORM值

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

I have an ASP.NET webpage written in C#. 我有一个用C#编写的ASP.NET网页。 In my master page, I have a <select> element which allows the user to switch between two different databases. 在我的母版页中,我有一个<select>元素,该元素允许用户在两​​个不同的数据库之间切换。 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. 我以为我可以将代码添加到母版页Page_Load()方法中,以检查包含选择内容的表单是否已提交,如果是,则设置一些会话变量来定义我要连接的数据库。

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. 问题是,它看起来像页面本身在母版页的Page_Load方法之前加载,因为当我提交表单时,它确实更新了会话变量,但是我的查询仍在查看原始数据库。 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. 我猜测母版页的Page_Load方法必须在页面的Page_Load方法之后触发。 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. 您可以调用findcontrol获取选择。

Master.FindControl("IDofYourSelect");

To get this working you should use a asp:dropdownlist instead of a select! 要使此工作正常,您应该使用asp:dropdownlist而不是select!

Now you can handle the connection selection inside your page-code just before you need the db-connection 现在您可以在需要db-connection之前处理页面代码内的连接选择

This can be accomplished by building your own IHttpModule implementation and registering it in your web.config file. 这可以通过构建自己的IHttpModule实现并将其注册到web.config文件中来实现。

https://msdn.microsoft.com/en-us/library/ff649096.aspx https://msdn.microsoft.com/zh-CN/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. 在ASP.NET WebForms中,始终从最内层向外处理Page事件。 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. 见在回答这个职位页面生命周期的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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