ASP.NET CheckBox and PostBack Account Status

I have a simple task - to change the link to the link to change the state of the checkbox, but I am new to ASP.NET and have some problems.

I can do the same with 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; } </script> <form action="" method="get"> <input type="checkbox" name="checkbox" onload="checkbox_onLoad(this)" onchange="checkbox_onChanged(this.checked)" />Show all </form> 

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

 <asp:CheckBox runat="server" ID="check" OnCheckedChanged="check_CheckedChanged" 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 a change in the check state at boot again triggers an event, and the check box is always checked!

Another question - is there another way to redirect to one page without specifying a file name? I mean, for example, in JavaScript - are only variables needed?

+4
source share
1 answer

You can call the checkbox_onChanged checkbox on the client side from the ASP.NET checkbox, just add onchange from the Page_Load page, for example:

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

Take a look at the source and you will see what happens in the HTML.

+2
source

All Articles