2

I have a checkbox from where I get the checked value but the first time works great but then I doesn't change at all after postback and always returns true.

I'm just doing this

bool accepted = this.chkAccepted.Checked;

My checkbox is inside a control. Not repeater not directly in a page.

<asp:CheckBox ID="chkAccepted" runat="server" Checked="true"/>Accepted

The first time it starts checked = true. I click my button first postback and work fine, then I uncheck, click my button but the checked is still true.

The first time it starts checked = true. I uncheck the checkbox and I click my button first postback and work fine, then I check, click my button but the checked is true, then I uncheck again and is always checked = true.

So, what is the bug for this?

I have another checkbox in the same control which has no Checked property initialized and always works fine. So how can I solve this problem please?

Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • 1
    http://stackoverflow.com/questions/1523606/asp-net-checkbox-value-at-postback-is-wrong?rq=1 – Vland Dec 27 '13 at 17:37
  • @Vland, Thanks for the articule. I found it is a current bug in .Net and as I stated before it works only when the property checked is not defined in the aspx. I found a solution very simple for this. Thank you. – Maximus Decimus Dec 27 '13 at 18:45

1 Answers1

5

Ok, I realized that it's a .NET bug after some research. So when the property Checked of the checkbox is set to true in the aspx, this causes the problem. So, I removed this property and in the Page_Load event(server side) I initialized the checkboxes as true inside a Page.IsPostBack == false. And that's solved my problem.

Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • Not sure about the bug you refer. If I create a new app, write your code, add the suggested AutoPostBack=true I see how the code behind is reached with the right value everytime the user clicks on the checkbox. I have preferred to delete my answer (a bit tired of peculiar behaviours in SO and all this started to sound familiar...). – varocarbas Dec 28 '13 at 08:38
  • 3
    Just for future readers (and, if you wish, for me to confirm that this is a "peculiar situation" or not -> I would ask for apologises in case of being required). Wouldn't you mind to write your whole code (the relevant parts, I mean) together with its exact applicability? This whole question (and its answers) might be really confusing for unexperienced ASP developers (it is confusing to me and I have some experience). Remember that the whole point of SO is delivering clear and reliable information which might be useful to someone. – varocarbas Dec 28 '13 at 12:07
  • It's so simple, Just add an Asp.Net checkbox control in a page and enable the property checked="true" (NO AUTOPOSTBACK. There's no need to postback anytime to click on the checkbox) Then add a Asp.Net button with the click event then, as I did, declare a boolean variable to assign the checkbox checked attribute. Debug the multiple values of the checkbox and you will find it. This does not occurs when you set autopostback=true, but I don't need to postback all the time I change the state of the checkbox. – Maximus Decimus Dec 30 '13 at 18:05
  • 2
    Yes, I got that. What I didn't get was the bug. What you did? Why this does not work right away? (posting the code is pretty quick: the "before" (bug) and the "after" (your correction)). Also you should include this last comment in your answer: future readers might not understand immediately that you do need to check the checkbox state every time because without AutoPostBack (or equivalent means), the code behind does not know about changes in the checked state (take a look at the answer you get from user3140451, convinced that it is recommendable to not use AutoPostBack). – varocarbas Dec 30 '13 at 18:08