1

I have problem with determine if checkbox is checked on my webpage build in ASP.NET. When I render webpage in Page_Load method I check some of checkbox using code like:

checkbox1.Checked = true;

On page I have button to process data in form, on click in this button i check if checkbox was checked:

if(checkbox1.Checked == true)
{
    //do something
} else {
    //do something else
}

But i found that value which I get from Checked property is always same as I set in first step. That property never returns value of current state of CheckBox. And here is my question: Why?

I menaged to bypass that problem by setting checked in different way:

checkbox1.Attributes.Add("checked","true");

I don't like when something works but I don't know why so please give me advise what I'm misunderstanding.

5
  • Not sure who voted to close this as a typographical error. It might be a trivial error, but that doesn't mean it's typographical. Commented Mar 4, 2015 at 17:38
  • @mason, it wasn't my vote, but maybe the one who did it meant the first part? problem that can no longer be reproduced The code as-is is kinda ok and we can't tell for sure what's wrong. It's most likely something really trivial that could have been resolved by simply reading through the most basic tutorials... Commented Mar 4, 2015 at 17:40
  • @walther I don't think that makes anymore sense than typographical close reason. It is a reproducible problem. If someone feels there's no enough code here, it should be the "Questions seeking debugging help..." close reason. But you and I obviously thought there was enough code here to answer this question, since it's a common mistake and we've seen it before. Commented Mar 4, 2015 at 17:43
  • @mason, you have to admit we're purely guessing the source of the problem and that's the main problem of this question. It should be more clear and easily reproducible. In its current form it simply isn't. We can provide a guess and some tips, but it very well may come to the situation when we both have to delete our answers, because there may be something else going on he's not showing... Commented Mar 4, 2015 at 17:45
  • @walther This question is to help future people that have this problem too. Whether or not it solves OP's exact issue may not be true, but others may come across this question with the same problem but for different underlying reasons, and our answers might help them. Anyways, seems we both guessed right for solving the OP's particular problem since your answer got accepted. Commented Mar 4, 2015 at 17:48

2 Answers 2

2

In your Load event you're probably missing part with IsPostBack, so it executes every time and end up rewriting your values.

protected void Page_Load(object sender, EventArgs e)
{
      if (!IsPostBack)
      {
           // do initial initialization here
      }

      // this happens even during a postback
}
Sign up to request clarification or add additional context in comments.

Comments

2

You do know that Page_Load runs after every postback, before it executes your control event handlers such as Button_Click? You're probably resetting the values every time the page loads. You should write your initialization code inside an if statement to make sure you don't overwrite the value that was posted back.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        checkbox1.Checked = true;
    }
}

Also,

if(checkbox1.Checked == true)
{
    //do something
} else {
    //do something else
}

Since the checkbox1.Checked value is a Boolean already, there's no need to compare it to true. When you read the code verbally it's like saying "If the checkbox is checked, and the previous statement I just said is true". You can shorten it, so that it reads more like "If the checkbox is checked".

if(checkbox1.Checked)
{
    //do something
} else {
    //do something else
}

2 Comments

As I'm mainly C programmer I don't like use ifs without comparing because it leads to lot of problems in C.
@KotMorderca C# is not C. You should embrace it. Not that it makes a difference as far as execution here, but it will improve readability and brevity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.