Click here to Skip to main content
14,361,664 members
Rate this:
Please Sign up or sign in to vote.
See more:
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...
Posted
Updated 31-Aug-15 4:45am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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;
                }
            }
        }
   
v3
Comments
Member 11864926 2-Sep-15 1:19am
   
now i have changed my code to

if (!IsPostBack)
{
var MasterPage = Master;
var a = Request.UrlReferrer;
if (a != null)
{
if (MasterPage != null)
{
MasterPage.FindControl("Admin_Header1").Visible = false;
MasterPage.FindControl("tdLeft").Visible = false;
MasterPage.FindControl("footertable").Visible = false;
}
}
}

but still when the page is loaded directly it finds the var a as null and does not show the header...

Please help me...
arindamrudra 2-Sep-15 3:38am
   
Please check my updated answer.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
}
   
Comments
Member 11864926 3-Sep-15 1:30am
   
thank you so much for your reply...i did the same thing but now for the first time its hitting the session_start....so its working for the first time..as soon as i go back and forth between pages it never go into else condition... and hence not getting the required output...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100