Dynamic PostBackUrl from JavaScript

Posted on 2008-11-13
Last Modified: 2013-11-19
I have a ASP.NET page that is updated dynamically via WebServices and JavaScript. There is a LinkButton that needs to go to 1 of 2 pages depending on the state of the page when it is clicked. This is contained in a MasterPage with hidden fields that need to be maintained so It needs to do a PostBack. How do I either change the PostBackUrl of the LinkButton each time a WebService updates the page or determine which page to redirect to when the button is clicked?
Question by:DerekWatling
    LVL 7

    Accepted Solution

    I believe the best result is to use JavaScript:
    1. Set the link button to have only OnClientClick="Navigate(); return false;" and no OnClick so there will not be any postback.
    2. Use the following JavaScript function code, and setup your parameters (hidden-field name, value, and pages' URLs)
    function Navigate()
      var hiddenField1 = document.getElementById("<%= hiddenField1.ClientID %>");
      if (hiddenField1.value == "Something")
        location.href = "Page1.aspx";
        location.href = "Page2.aspx";

    Open in new window


    Author Comment

    This works for redirecting, but I need a Cross Page PostBack as I am using HiddenFields on the MasterPage to track session variables.

    After trying various other things I have decided the easiest is to have 2 Image buttons on the page each with a static PostBackUrl and set the display property on the client so that only the relevant button is displayed.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now