0

I am trying to do something that I'm not sure is possible or not?

The user opens the website. If they try to reopen the link in a new tab I need to redirect them to another page.

I've tried using c# to see if I can detect a page reload. This works, but if I put my code in the page load event for some reason it runs twice, and it redirects them no matter if it's the first time they opened or if they are opening in a new tab. I need to only happen if they open a new tab.

My Code:

bool IsPageRefresh = false;
if (!IsPostBack)
{
  ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
  Session["SessionId"] = ViewState["ViewStateId"].ToString();
}
else
{
  if (ViewState["ViewStateId"].ToString() != Session["SessionID"].ToString()) ;
  {
    IsPageRefresh = true;
    if (IsPageRefresh == true)
    {
      Response.Redirect("~/Pages/EstimateList.aspx", true);
    }
  }
  Session["SessionId"] = System.Guid.NewGuid().ToString();
  ViewState["ViewStateId"] = Session["SessionId"].ToString();
}

Do I need to use Javascript or jQuery to do this? I'm really new to Javascript and jQuery.

Huy Nguyen
  • 2,025
  • 3
  • 25
  • 37
hollyquinn
  • 652
  • 5
  • 15
  • 48
  • 2
    While you may be able to hack something together to do this, all you are probably going to do is irritate the user by doing something they are not expecting. – DavidG Jul 29 '16 at 13:22
  • I doubt you can do this. Certainly not server side and I'd imagine the JS will restrict this functionality in the browser as it's annoying and can be abused – Liam Jul 29 '16 at 13:23
  • I didn't get what you want. Does it matter if the user type again in the address bar or you just don't want to have 2 tabs of the same page to be open? – Ashkan S Jul 29 '16 at 13:28
  • Ashkan, I just don't want to allow them to open it in a new tab. – hollyquinn Jul 29 '16 at 13:30
  • You realise what a horrible user experience that is right? – DavidG Jul 29 '16 at 13:30
  • @Liam If it can't be done it can't be done. I just needed to check if it was possible. Thank you. – hollyquinn Jul 29 '16 at 13:31
  • @DavidG Yes I do. Lol. They want to do it this way though. – hollyquinn Jul 29 '16 at 13:32

2 Answers2

1

You can use cookies. When the user first open the page you send a cookie to the computer saying PageOpened=true , when the user opens it in the new tab check if there is a cookie with PageOpened=true, if there is, open your other link in the browser.

You can give your cookies a time limit or destroy them when the first site is closed or when the user opens the new tab etc..

I can't provide you the code as I never worked with c# on web but this should give you an idea on possible routes to the solution.

P.S.: I would also find it irritating as a user but that is your call.

atakanyenel
  • 1,367
  • 1
  • 15
  • 20
  • Can it be done in Javascript? – hollyquinn Jul 29 '16 at 13:33
  • [w3schools](http://www.w3schools.com/js/js_cookies.asp) and [stackoverflow](http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) on cookies with javascript – atakanyenel Jul 29 '16 at 13:36
0

you can try this

if(Request.Cookies["isPageRefresh"] == null)
{
   Response.Cookies["isPageRefresh"].Value = "true";
   Response.Cookies["isPageRefresh"].Expires = DateTime.Now.AddDays(1);

}
if (!IsPostBack) 
{                  
 if (Server.HtmlEncode(Request.Cookies["isPageRefresh"].Value== "false") 
 { 
   Response.Redirect("~/Pages/EstimateList.aspx", true);
 } 
 //enter some code
}
  Response.Cookies["isPageRefresh"].Value = "false";
  //enter some code

because on each new tab click asp understand it as a new request. you should use cookies for checking isPageRefresh if false redirect or use global variable that doesn't have redirect effect like cache variable value

Suyash Kumar Singh
  • 173
  • 1
  • 2
  • 10