oncheckedchanged event is not firing everytime on checkbox

H

Hitesh Jain

Hi,
I have a checkbox control inside a datalist control's item template and in
the html i am associating the event handler with the oncheckedchanged event
for the checkbox like the following:
OnCheckedChanged="OnCheckedItem"
now when i am running the ASP.NET application and say at runtime 3 checkbox
gets generated based on the data from my database, and if i checked the first
checkbox the event is firing but now when i uncheck the same checkbox the
event is not firing.
Can anybody help me to get a resolution or workaround for this problem.
If you need any further details then let me know so that i can provide the
same.
Thanks
Hitesh
 
R

Robert Koritnik

What you've written isn't exactly explaning the real work done behind the
scenes. Is checking unchecking done at once on the client side or is there a
postback inbetween?

Anyway. I can answer on both.
A. Twice checking/unchecking on the client without postback
I think this is pretty obviews why there is no checked change event here,
because to the server the unchecked "went out" and uncheked "came in".
Sometimes this behavior should fire server events i.e. multipage selects and
one global selection - when global selection is clicked all selects on all
pages (visible page + other) should be selected. One checkbox is deselected
on the current page, global should uncheck, but other pages must stay
checked. This is normal behavior, and you have to do this with another
server control (must have more than two states), best with hidden field
check uncheck other pages.

B. First check, then postback and uncheck and postback.
The problem that you're having is ViewState. Let's examine the process:

1. you get a checkbox on the client and check it
2. postback.
3. your checkboxes are regenerated on the server and change events are
executed and everything goes back to the client with viewstate info
supplied.
4. you uncheck the checkbox on the client
5. postback
6. step by step
- parse the aspx
- on init fires
- views state loads the state of the page as it was sent to the client
on previus request
- postback data changes values to new ones and prepares change and
action events as well
- on load fires
- change events fire
- action event(s) fire
- no need for me to go any furter. after this pre render, saving view
state, render and unload.

ok so where seems to be the problem? Your page gets parsed. No checkboxes
yet while you create them dynamicly. Ok. on init fires, last page view state
loads and doesn't update your checkboxes, so they stay unchecked. When
change events should fire, your checkboxes don't fire them, while postback
data wasn't passed to them.

the reason for this behavior is that you create your checkboxes too late as
in page load event.

I just hope I didn't mess things up this time, while already 11 hours
straight at work. :( Going home now. Good night.
 
H

Hitesh

Hi Robert,
Thanks for you indepth analysis of the problem and yes there is a postback
between checking and unchecking, i had made the autopostback property of the
checkbox to true, but now the question with us is how to solve the problem
can you help me on this futher...?
Thanks a bunch for the description.
 
H

Hitesh

Hi Robert,
Success! great thanks from me, with the help of your knowldge i could able
to manage the things. The trick is, i did the coding in prerender event for
the processing of checkbox's changes which initially i was doing in the event
handler for the oncheckedchanged event of the checkbox. so now things are
working and i am able to manage the checking and unchecking of child check
boxes based on the checked state of the parent check box, only drawback with
this is the code is executed always as prerender fires on every postback
whether its because of the checkbox or because of any other control. But in
my case the page is only having check boxes so celebrating the success!
Thanks a lot for all the geniuses who have spent their time on this problem,
and special thanks to Robert.

Cheers!
Hitesh
 
R

Robert Koritnik

You didn't have to put things in PreRender, which is more like a
workarround. One thing is that you should have your checkboxes prepared as
late as in OnInit. If this would be a custom server control,
CreateChildControls would be the best one to go for. That way ViewState will
be preserved as well as PostBack processing and all events.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top