如何检查页面是直接加载还是从asp.net中的另一个页面重定向 [英] How to check the page is loaded directly or being redirected from another page in asp.net

查看:98
本文介绍了如何检查页面是直接加载还是从asp.net中的另一个页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一页品牌大师我可以直接加载,或者我可以通过其他页面重定向。

当页面被重定向时,它应该隐藏其包含的标题以及何时加载直接它应该显示标题部分

但当我尝试做这样的事情

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
var MasterPage = Master;
if (!string.IsNullOrEmpty(Request.UrlReferrer.ToString()))
{
MasterPage.FindControl( Admin_Header1)。Visible = false ;
MasterPage.FindControl( tdLeft)。Visible = ;
MasterPage.FindControl( footertable)。可见= ;
}
}
}





并尝试直接打开页面它因为request.urlreffer为空而抛出错误..



任何人都可以帮我解决这个问题...

解决方案

你不应该在那里使用.ToString()。而不是你可以使用:



  if (!string。 IsNullOrEmpty(Request.UrlReferrer))





或者你可以使用

 Convert.ToString(Request.UrlReferrer)



它处理空值,这是ToString()和Convert.ToString()之间的主要区别。



使用以下代码解决您的目的:



  if (!IsPostBack)
{
var MasterPage = Master;
var a = Request.UrlReferrer;
string url = HttpContext.Current.Request.Url.AbsoluteUri;
if (a!= null && url == 您的必填网址
{
if (MasterPage!= null
{
MasterPage.FindControl( Admin_Header1)。Visible = false ;
MasterPage.FindControl( tdLeft)。Visible = ;
MasterPage.FindControl( footertable)。可见= ;
}
}
}


更改



  if (!string.IsNullOrEmpty(Request.UrlReferrer.ToString()))





to



  if (Request.UrlReferrer!=  null 





然而这是怎么回事逐页工作,这可能不是你想要的。例如,如果您通过其他网站访问该网站,则会隐藏标题。但是,如果我在地址栏中按[enter]重新加载页面,则标题将再次显示。如果你想记住网站首次加载的方式并坚持使用该配置,那么在global.asax.cs文件中添加一个事件



  void  Session_Start( object  sender,EventArgs e)
{
if (Request.UrlReferrer == null
{
Session [ 直接] = true ;
}
else
{
会话[ 直接] = false ;
}
}





然后在你的代码中



  if (( bool )会话[ 直接])
{
// direct so show header
}
else
{
// 重定向,因此隐藏标题
}


I have one page of brand master which i can load directly or i can be redirected through other page.
when the page is redirected it should hide the header which its containing and when it is loaded directly it should show the header part also
but when i am trying to do something like this

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           var MasterPage = Master;
           if (!string.IsNullOrEmpty(Request.UrlReferrer.ToString()))
           {
               MasterPage.FindControl("Admin_Header1").Visible = false;
               MasterPage.FindControl("tdLeft").Visible = false;
               MasterPage.FindControl("footertable").Visible = false;
           }
       }
   }



and trying to open the page directly when its throwing an error because request.urlreffer is null..

can anyone please help me out with this...

解决方案

You should not use .ToString() over there. Instead of that you can use:

if (!string.IsNullOrEmpty(Request.UrlReferrer))



Or you could have use

Convert.ToString(Request.UrlReferrer)


It handles null value and that is the main difference between ToString() and Convert.ToString().

Use the below code to resolve your purpose:

if (!IsPostBack)
        {
            var MasterPage = Master;
            var a = Request.UrlReferrer;
            string url = HttpContext.Current.Request.Url.AbsoluteUri;
            if (a != null && url == "your Required url")
            {
                if (MasterPage != null)
                {
                    MasterPage.FindControl("Admin_Header1").Visible = false;
                    MasterPage.FindControl("tdLeft").Visible = false;
                    MasterPage.FindControl("footertable").Visible = false;
                }
            }
        }


Change

if (!string.IsNullOrEmpty(Request.UrlReferrer.ToString()))



to

if (Request.UrlReferrer != null)



however this is going to work on a page by page basis which might not be what you want. Eg if you come to the site via another site then the header will be hidden. However if I press [enter] in address bar to reload the page then the header will show again. If you want to remember how the site was first loaded and stick to that configuration then add an event in your global.asax.cs file

void Session_Start(object sender, EventArgs e)
{
    if (Request.UrlReferrer == null)
    {
        Session["Direct"] = true;
    }
    else
    {
        Session["Direct"] = false;
    }
}



then in your code

if ((bool)Session["Direct"])
{
    // direct so show header
}
else
{
    // redirected so hide header
}


这篇关于如何检查页面是直接加载还是从asp.net中的另一个页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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