Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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 3:45am
v2

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

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


Or you could have use
C#
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:

C#
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;
                }
            }
        }
 
Share this answer
 
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.
Change

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


to

C#
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

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


then in your code

C#
if ((bool)Session["Direct"])
{
    // direct so show header
}
else
{
    // redirected so hide header
}
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900