Search code examples
c#asp.netlinkbuttononclientclick

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


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!?


Solution

  • 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