I have some checkboxes on a page. I get them using FindControl() in an UpdatePanel after pressing a button trigger, but the checked value is wrong. How can I get the correct checked value?
1 Answer
If you have any code that sets the values of the checkboxes on your page, make sure it isn't executing on postbacks, like this:
protected void Page_Load(object sender, EventArgs e) {
// Only set the checkboxes on GETs, not on POSTs
if (! this.IsPostBack) {
this.EmailMeUpdatesCheckbox.Value = false;
}
}
Actions triggered within UpdatePanels still go through the page lifecycle (which is why you have access to all your Page's state), so it may be clearing the user's selections before getting to the code in which you examine the checkbox values.
4 Comments
NibblyPig
Page init only ever runs once, and it is in a !isPostback if statement.
Jeff Sternal
That's not correct - Page_Init runs every time you post back. Having said that my original example was silly since the values set in Page_Init would get overwritten when the posted form values are mapped back to controls. Do you have any code that's setting your checkbox values on the page? You may have to post your code for us to help debug this. At what point in the page lifecycle are you attempting to read the checkbox values? Are the checkboxes generated dynamically?
NibblyPig
Posting any code wouldn't really help... my research seems to indicate that basically, the checkbox values aren't set until the prerender method or something similar runs, and that doesn't run until AFTER my trigger method. This was confirmed by clicking the button twice, which caused it to remember which checkboxes were clicked. I will consider your reply and check again to make sure nothing is resetting the values though
Mike C.
I agree with Jeff: without posting some code, there is no what to know what or where the problem is. Let's have some code!