views:

99

answers:

4

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?

+1  A: 

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?

RandomNoob
I'm not seeing any js errors in the error console. I did try using a Trigger and it had the same effect but I have also removed the update panel altogether and it still has the same problem.
Brandon
Are you setting autopostback to true at runtime via code or markup?
RandomNoob
I set that in the markup.
Brandon
Are you concerned it's not posting back each time? I can hit a break point in the OnLoad event handler every time I click the checkbox so I know it's posting back but every other time the checkchanged event gets raised...
Brandon
I went ahead and tried to set the AutoPostBack to true via code just to see if that made a difference and it didn't change anything.
Brandon
@Brandon, see update...not sure it will fix, but wanted to confirm
RandomNoob
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.
Brandon
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...
Brandon
Okay. Just out of curiousity have you tried manually setting the event handler on Page load (don't include within if (!Page.IsPostBack)) - so that it hooks up every single time // Manually register the event-handling method for the yourCheckBox.CheckedChanged += new EventHandler(yourhandler)Just curious at this point.
RandomNoob
Yeah, I did try that before. I went ahead and tried it again to make sure, but that didn't work either.
Brandon
A: 

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.

Streamcap
A: 

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.

Junto
Yes, I checked this out and it wasn't the case. Thanks for the suggestion, though.
RexM
A: 

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.

RexM