如何在母版页加载时以编程方式决定加载哪个内容页
我有一个母版页和两个具有相同 ContentPlaceHolderID 的内容页。有没有办法指定应从母版页的 Page_Load
事件加载哪个内容页?
如果我观察以下值:
Request.CurrentExecutionFilePath;
我会看到第一个内容页面的路径。
根据下面指定的条件,我想将其更改为第二个内容页面的路径。
我正在寻找一种方法来加载特定页面,具体取决于我在主上所做的检查页面加载
。
如果我尝试从那里重定向到该页面,我就会陷入无限循环,因为母版页会在内容页面之前再次加载,并一次又一次地重新进行检查和重定向。
// 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);
}
}
}
提前致谢。
I have a master page and two content pages with the same ContentPlaceHolderID. Is there a way to specify which content page should be loaded from the Page_Load
event of the master page?
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
.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查重定向中包含的查询字符串的页面加载情况。如果查询字符串在那里,那么您将已经重定向,因此您可以跳过 cookie 检查和重定向块。
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.