Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 6 checkboxes in HTML page. I set them to Checked = true if some condition exists:

C#
if (isMammals)
    mammals.Checked = true;
if (isBirds)
    birds.Checked = true;
if (isReptiles)
    reptiles.Checked = true;
if (isAmphibians)
    amphibians.Checked = true;
if (isFishes)
    fishes.Checked = true;
if (isInvertebrates)
    invertebrates.Checked = true;


But even when the user unchecks the checkbox Checked remains true:

C#
isMammals = mammals.Checked;
isBirds = birds.Checked;
isReptiles = reptiles.Checked;
isAmphibians = amphibians.Checked;
isFishes = fishes.Checked;
isInvertebrates = invertebrates.Checked;


What I have tried:

The value of this variables remains true. How can I fix it?
Thanks
Posted
Updated 22-Apr-23 8:26am

1 solution

From just that scrap of code, we can't tell - but at a guess it's because of how web pages work: are you checking to see if this is a postback in your page load event handler? Because if you aren't, then when the user checks his boxes and clicks "ENTER", the load event will fire again and set the check boxes - then the Click event will fire and you have lost the user set states.

Page.IsPostBack Property (System.Web.UI) | Microsoft Learn[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900