-1

Problem most likely is with the QueryString. Everything falls apart when I go back from results page to search page. I use the QueryString to check all boxes that were checked before the results page is generated. This time I can uncheck checked boxes, but only visually. The results page will be generated as if they were still checked.

Here is simple example of it. I have added checkbox and a button...

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["B1"] != null)
    {
        CheckBox1.Checked = true;
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    string QString = null;
    if (CheckBox1.Checked)
    {
        QString += "?B1=1";
    }
    Response.Redirect("/TestPage1.aspx" + QString);
}

If I go to Default.aspx and if I add "?B1=1"(Default.aspx?B1=1) I get page where checkbox is checked. If I uncheck it and press Button1, I will be redirected to page "TestPage1.aspx?B1=1"

Does anyone have a solution to this problem?

Scotty
  • 1,127
  • 1
  • 7
  • 17
dangtis
  • 3
  • 1
  • 3
  • Checkboxes and radiobuttons are pretty buggy in asp.net. Check out this thread: http://stackoverflow.com/questions/1523606/asp-net-checkbox-value-at-postback-is-wrong – Sal Aug 03 '16 at 01:53

2 Answers2

1

If I understand correctly, you are on TestPage1 with the checkbox checked (/TestPage1.aspx?B1=1) and when you manually uncheck the checkbox and click Button1 you return to /TestPage1.aspx?B1=1 and the checkbox is now rechecked.

This is occurring because the Page_Load event is raised before control events such as the OnClick event of Button1. Here is the MSDN documentation detailing the page lifecycle events.

Since your page is posting back to the /TestPage1.aspx?B1=1, the Page_Load handler sees the query string before the Button1_Click has a chance to remove it. By the time the Button1_Click handler runs, the checkbox has already been rechecked and the handler redirects back to the same url, query string included.

A simple solution for your example would be to to check the IsPostBack property in your Page_Load event and only check the checkbox if the page is not posting back.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack && Request.QueryString["B1"] != null)
    {
        CheckBox1.Checked = true;
    }
}
ttschumy
  • 38
  • 5
0

It goes like this:

  1. You have /TestPage1.aspx?B1=1 loaded in your browser.
  2. You uncheck CheckBox1 in the browser and click Button1
  3. Page_Load() runs on the server for the full /TestPage1.aspx?B1=1 address. It sees the B1 query string so CheckBox1 is set as checked again.
  4. Button1_Click() runs on the server. It sees CheckBox1 is now checked, so QString includes B1 again. The server now redirects to /TestPage1.aspx?B1=1 (the full query string is still included.
  5. Page_Load() runs again, and the CheckBox1 is once more set as checked.
  6. The page is finally sent back to the browser, where you observe Checkbox1 is now checked.

If you only have TestPage1.aspx in the browser (without the ?B1=1), you would be able to leave the checkbox unchecked. But as long as that ?B1=1 is there, the cycle of events is always going to put it back again.

The most common mistake here is not to expect step 3. Remember that any server events (even simple button clicks) run as part of the full page lifecycle. You can help this by changing Page_Load() to include an !IsPostBack() test before setting the checkbox.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794