2

I have a checkbox on a .ascx file. This .ascx file is located within a closed source web application and something is preventing the checkchanged event from firing every other time.

When I first load the page and click the checkbox (checking it) the first time the checkchanged event is fired. If I click it the second time (uncheck) the checkchanged event isn't fired. If I check it again the event is fired and when i uncheck it again it doesn't fire and continues as so. If I set the checkbox's default value to be true, as it loads up, the same effect occurs only it fires when it is unchecked the first time and doesn't fire when it is checked the second time and so on.

The page this .ascx is on has EnableViewState set to true as well as the .ascx and I've even tried setting it on the checkbox itself. The checkbox has the AutoPostBack set to true as well.

If I pull the .ascx file out of this application and put it on a test web application page it works as expected. I also better mention that the checkbox is inside of an update panel but that doesn't seem to be related because if I remove the update panel it still has the same adverse effect.

What within this application could be preventing the checkchanged event from firing every other time?

4
  • Is there any special javascript being output that is preventing the postback? Commented Jun 18, 2010 at 18:24
  • I know it posts back for sure both times. It's just on that second click it doesn't fire the checkchanged event and so on every other time. Commented Jun 18, 2010 at 18:28
  • I also tried to get around it by using the OnLoad event handler to check if it was a postback and if it was just look to see if the checkbox is checked or not but it always think's it's false.... Any idea why that would be? Commented Jun 18, 2010 at 18:41
  • I am having this exact same issue. My ascx is included in a repeater. When I select 1 item, the event fires once, 2 items, it fires twice (once for each item). When I uncheck one item, it will fire the event for the other checked checkbox. I'm sure it has something to do with the viewstate. Have you figured out the issue? I've added a bounty to the question. Commented Aug 20, 2010 at 17:14

5 Answers 5

2

Hmm, are you sure there are no other javascript errors? Also have you tried adding a trigger since you mentioned it was inside an update panel?

<asp:UpdatePanel>
    <ContentTemplate>
       //Content
    </ContentTemplate>
    <Triggers> 
        <asp:AsyncPostBackTrigger ControlID="yourCheckBox"  /> 
    </Triggers>
</asp:UpdatePanel>

Update: Have you tried forcing the autopostback property via code

CheckBox checkbox = yourFormOrContainer.FindControl(yourCheckBox.ID) as CheckBox;
if(checkbox != null)
{
     checkbox.AutoPostBack = true;
}

Second Update: Per Microsoft: A CheckBox control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for this control.

If you are persisting the value to the database, when the page/updatepanel posts back, are you pulling back its value, I suspect its getting its old value, so its still thinking its checked and hence not firing. If inside an update panel originally, are you using Bind to set its data value, presumably a bit field?

Sign up to request clarification or add additional context in comments.

2 Comments

I'm just toggling the checkbox from checked to unchecked and setting a label to "true" if checked or to "false" if unchecked. I'm not persisting anything to a database or getting the checkbox's value from a database. Also, it happens even if I remove the Update Panel.
It all works fine if I put this user control within a test application outside of this closed source application that I need it to work within. The closed source application is interfering with it somehow...I think...
0

This is me speculating: If I wanted to have a checkbox that behaved this way (for whatever reason) I would probably overload the class and then keep an int (or a status bool) that tracks/toggles with any changes, letting the event bubble up only if the bool was true/the int was divisible by 2.

(Check the http://odetocode.com/code/94.aspx code and imagine an if statement in the onclick method to see what i mean.)

As for keeping the counter/status variable, if it's not in Viewstate it might be hidden in ControlState, Session or even the Application cache. (BTW, try to list all those if you haven't already...)

Then again, this might be naïve but can it be that the "unchecked" event is triggered separately - and has to be handled in its own handler? I have seen stupider things... I think.

Comments

0

This is worth checking, because I've had similar problems.

You have confirmed that you have checked the EnableViewState attribute on your page and the Control itself. However, ViewState cascades, so if you have your control within a control, and the parent control has EnableViewState set to false, then the child control, even if it had ViewState enabled, would still have no ViewState.

I had a load of nested controls, and one of them had ViewState disabled and it bugged me for hours.

Comments

0

Currently we're in the process of slowly rewriting the application using FubuMVC. I have both fubu and webforms running in parallel, so I was able to rewrite this small section using fubu instead of trying to waste anymore time messing with web forms for this problem.

I know for most people, this wouldn't be an option, but this was how I solved it.

Comments

0

setting AutoPostBack="True" works for me

Comments

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.