简体   繁体   English

如何以编程方式决定在母版页加载时加载哪个内容页面

[英]how to programmatically decide which content page to load , on master page load

I have a master page and two content pages with the same ContentPlaceHolderID. 我有一个母版页和两个内容页面具有相同的ContentPlaceHolderID。 Is there a way to specify which content page should be loaded from the Page_Load event of the master page? 有没有办法指定应从母版页的Page_Load事件加载哪个内容页面?

If I watch the value of: 如果我看到的价值:

Request.CurrentExecutionFilePath;

I see the path of the first content page. 我看到了第一个内容页面的路径。

According to the condition specified below i want to change it to the path of the second content page. 根据下面指定的条件,我想将其更改为第二个内容页面的路径。

I am looking for a way to load a specific page depending on a check I do on the Master Page_Load . 我正在寻找一种方法来加载特定的页面取决于我在Master Page_Load上的检查。

If I attempt to redirect to the page from there I get stuck in an endless loop because the master page loads again before the content page and re-does the check and redirects again and again. 如果我尝试从那里重定向到页面,我会陷入无限循环,因为母版页在内容页面之前再次加载并重新进行检查并反复重定向。

// in master page 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Request.Cookies["user"] != null)
        {
            HttpCookie cookie = Request.Cookies["user"];
            string name = cookie.Value;
            Response.Redirect("~/hello_page.aspx?UserName=" + name);
        }
    }
}

Thanks in advance. 提前致谢。

You could check on page load for the querystring you're including in the redirect. 您可以检查页面加载以查找重定向中包含的查询字符串。 If the querystring is there, then you will have already redirected, so you can skip over the cookie-check-and-redirect block. 如果查询字符串在那里,那么您将已经重定向,因此您可以跳过cookie-check-and-redirect块。

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

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