Setting window.location on a LinkButton's onClientClick from code behind

user1017882 picture user1017882 · Jan 16, 2012 · Viewed 7.1k times · Source

Can't be this hard can it!? I just want to change window.location onclientclick of a linkbutton and set this from code behind.

lb.OnClientClick = "window.location = 'Contact.aspx'";

Not working, just reloads the current page.

lb.OnClientClick = "window.location = '" + Server.MapPath("Contact.aspx") + "'";

Seems to resolve the url correctly (the dev folder on my C drive) but is denying me access!?

Answer

Hogan picture Hogan · Jan 16, 2012

Example of how to use dynamically:

if (status = "fun")
  HttpServerUtility.Transfer("fun.aspx");
else
  HttpServerUtility.Transfer("sad.aspx");

also this should work

lb.OnClientClick = "window.location = 'Contact.aspx'; return false;"

Original post:

If it is in the code behind just use Transfer

HttpServerUtility.Transfer("Contact.aspx");

and this will pass all the form information:

HttpServerUtility.Transfer("Contact.aspx",true);

MS also has good documentation on all your options here