Search code examples
asp.netvb.netcheckboxautopostback

ASP.NET: How do I navigate to a page after clicking a checkbox?


I have some checkboxes and a dropdownlist when the value changes I want to refresh the page while passing the new value. I tried using autopostback; however, the value is already in the url previously so when the postback occurs the value never changes.

Example:

CurrentPage: page.aspx?tab=Home&checkbox=True

Then I uncheck the checkbox so I want it to go to the following page...

IntendedPage: page.aspx?tab=Home&checkbox=False

But instead I the autopostback gives me this page...

DestinationPage: page.aspx?tab=Home&checkbox=True

Because, I handle the building of the url through a function on my page. Perhaps I'm doing something wrong by this point. If so I'd be happy to be corrected on my current setup. What I think I need to know though is how to load a custom URL on the checkbox.checkchanged event. I hope this made sense, if not let me know I'll try and clarify it. Thanks!


Solution

  • This is very easy to do through javascript. First add onclick="checkCheckBox(this);" to your checkbox.

    <script language = "javascript" type="text/javascript"> 
    function checkCheckBox(checkbox) {
            if (checkbox.checked) {
                window.location.href = '../page.aspx?tab=Home&checkbox=True';
            }
            else {
                window.location.href = '../page.aspx?tab=Home&checkbox=False';
            }
        }
    </script>
    

    This should do it pretty easily.