0

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 1

2

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.

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

4 Comments

Page init only ever runs once, and it is in a !isPostback if statement.
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?
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
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!

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.