I have a simple task - to change page link on checkbox state change - but I'm new to ASP.NET and have some trouble.

I can do the same using HtmlControl and JavaScript:

<script type="text/javascript" language="javascript">
  function checkbox_onChanged(checked) {
    if (checked) {
      document.location = '?type=request_in&show=all';
    else {
      document.location = '?type=request_in&show=unapproved';

  function checkbox_onLoad(checkbox) {
    checkbox.checked = true;

<form action="" method="get">
<input type="checkbox" name="checkbox"
  onchange="checkbox_onChanged(this.checked)" />Show all

but I want to hide it from users. So I do:

<asp:CheckBox runat="server" ID="check" 
  AutoPostBack="True" Text="Show all" />

protected void check_CheckedChanged(object sender, EventArgs e)
  Response.Redirect(String.Format("{0}?type=request_in&show={1}", Request.Path, 
  checkViewRequestIn.Checked ? "all" : "unapproved"));

protected void Page_Load(object sender, EventArgs e)
  var show = Request["show"];
  if (!String.IsNullOrEmpty(show) && String.Equals(show, "all"))
    checkViewRequestIn.Checked = true;

But it seems that check state change on load raises the event again and checkbox becomes checked always!

Ans another question - is there any other way to redirect to the same page without giving it's filename? I mean like in JavaScript - only giving variables needed?

Was it helpful?


You can call your client-side 'checkbox_onChanged' from the ASP.NET checkbox, just add the 'onchange' from the Page_Load, e.g:

protected void Page_Load(object sender, EventArgs e)
   check.Attributes["onchange"] = "checkbox_onChanged(this.checked)";

View source and you'll see what is happening in the HTML..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow