I have a problem where I have a page with a checkbox :
<asp:CheckBox runat="server" ID="chkSelectAll" OnCheckedChanged="chkSelectAll_CheckedChanged" EnableViewState="true" Text='<%# CommonFunctions.GetText(Page, "CreateTask", "chkSelectAll.text") %>' AutoPostBack="true" />
First I had the problem that when the event was raised the checkbox wasn't keeping the checked property value. This is still strange because nowhere in the application we set the EnableViewState to false and by default it should be true. Well this behavior was fixed by puting EnableViewState="true".
So now, I have a checkbox that when I first load the page, wworks correctly. I check the box, the event is raised Checked property valu is good and the method is executed. Postback completes and renders the page. I check the box back to false, the event is not triggered and the page is reloaded with the previous data but the Checked Property stays at true.
So after the first postback I seem to loose behavior of event and ViewState.
We suspect the fact that we move dynamically the controls in the page (See answer here)
But this suspicion doesn't explain why everything works perfectly after the first load and stops working after first PostBack.
EDIT
I created a test project where you can experience my problem. At load if you check any checkbox both raise their respective event. After postback only the second raise both events. I seem to have a problem when I register the events after moving control.
-
It sounds like you are over-writing the checkbox data on postback. Set up your dynamic controls in the method that handles the page load event like this:
void Page_Load(){ if (!Page.IsPostBack) { /* call methods to render a clean page with fresh controls */ } }UPDATE - after sample code was posted
I was correct in principle however after seeing your code sample I am better able to explain:
In your User Control you have methods being called on Page Load which need to be told whether or not the control is being loaded for the first time or on a post back - otherwise, it just keeps rebuilding a fresh copy of the control.
Here is the modified code from your file "CollapsiblePanel.ascx.cs":
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { for (int i = 0; i < Controls.Count; i++) if (Controls[i] is CollapsiblePanelExtender) while (i + 2 < Controls.Count) { SearchUpdatePanel(Controls[i + 2]); plhContent.Controls.Add(Controls[i + 2]); } } }With this change, the checkbox holds it's value after the postback and your event is fired on every change.
Ben Scheirman : That's a really bad place to put logic. It is common to initialize the page in an if(!IsPostBack) block, but doing the else portion just encourages you to put your event handling code directly in the page load method.lucian.jp : No, the control isn't receiving any data, default value false, Text is a binding data, but it's text. Nothing in the pageloadRob Allen : @Ben - I understand your concerns. I wanted to show both possible code paths in case the reader was unfamiliar with the usage.Rob Allen : revised the code sampleRob Allen : @Lucian - I think the issue is you are resetting the controls to their defaults as the page builds, thus losing the user data. If you are not building the controls on load then I am incorrect.lucian.jp : @Rob I confirm I do not build my control on load. It is only defined in the ascx.Rob Allen : @Lucian - after reviewing your code sample, I have updated my response. I was right the first time.lucian.jp : @Rob I tested what you state and unfortunatly all it does is stop the dynamic move of the controls. After PostBack we see that the checkboxs are no longer inside the placeholder which has the background color blue. Because we actully need to move the control even if IsPostBackRob Allen : Then I must still be misunderstanding the purpose of the code. It appears that this is one pit stop in a line of things. Perhaps if you made a single question that covered your larger goal we could be of more help, rather than trying to assist so close up. -
I have a solution where Page_Load seemed not to be the correct to place my moving code. Moving it to CreateChildControls seems to have fix my issue :
protected override void CreateChildControls() { for (int i = 0; i < Controls.Count; i++) if (Controls[i] is CollapsiblePanelExtender) while (i + 2 < Controls.Count) { SearchUpdatePanel(Controls[i + 2]); plhContent.Controls.Add(Controls[i + 2]); } base.CreateChildControls(); }Thank you very much all who tried to help (for this instance Rob)
0 comments:
Post a Comment