在主页和内容页Page_Load事件 [英] page_load event in Master and Content Pages

查看:155
本文介绍了在主页和内容页Page_Load事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是其中当母版页合并与内容页事件发生的顺序:

Here is the sequence in which events occur when a master page is merged with a content page:

http://msdn.microsoft.com/en-us/library/ dct97kc3.aspx

所以,我的问题是:

我有一个登录页面(不使用母版页),一个母版页,数百内容页。

I have one login page (not use master page), one master page, and hundreds of content page.

我检查的登录会话会话[与loggedInUser] 在母版页(如果尚未登录,重定向到登录页面)

I check login session Session["loggedInUser"] in master page (if not logged in, redirect to login page)

所以,当我不登录,如果我输入一个内容页面的地址,则必须在母版页签登录会话和重定向到登录页面,对不对?但有两种情况在这里:

So, when I don't log in, if I type the address of one content page, it must check login session in master page and redirect to login page, right? But it has two cases here:

如果在内容页,我不使用相关的任何会话[与loggedInUser] ,会重定向到登录页面,因此,这是确定在这里!

If in content page, I don't use anything related to Session["loggedInUser"], it will redirect to login page, so, it's OK here!

第二种情况:如果我使用会话[与loggedInUser] 在例如内容页显示用户名:

The second case: if I use Session["loggedInUser"] to display Username in content page for example:

UserInfo loggedInUser = (UserInfo)Session["loggedInUser"];

它会回到这里空的对象,因为在内容的Page_Load 页前的Page_Load 打响了母版页,所以它thows而不是重定向到登录页面空对象。

it will return null object here, because the page_load in content page is fired before page_load in master page, so it thows null object instead of redirecting to login page.

我也试过 Page_ preINIT 在母版页,但没有帮助

I also tried Page_PreInit in master page but no help

protected void Page_PreInit(object sender, EventArgs e)
{
    if (Session["loggedInUser"] == null)
    {
        Response.Redirect("~/Login.aspx");
    }
}

任何建议?

推荐答案

最后,我想出了一个解决方案:

Finally I've come up with a solution:

我创建一个类的BasePage 是这样的:

I create a class BasePage like this:

public class BasePage : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
        if (Session["loggedInUser"] == null)
        {
            Response.Redirect("~/Login.aspx");
        }
        base.OnLoad(e);
    }
}

在内容页面,而不是从继承和,我改为的BasePage 和它完美的作品

And in the content page, instead of inheriting from Page, I change to BasePage and it works perfectly

感谢您的支持

尼斯日;)

这篇关于在主页和内容页Page_Load事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆