Collapsing a CollapsiblePanel programmatically, or "How to condescend"
Today's post brought to you by honorary Hillbilly, Kevin Hemeon, who sent the following e-mail:
Dear Coding,
I realize you're a big mucky-muck who is too busy for the likes of the little people but my job, my life, indeed my very *soul* depends on being able to collapse an ASP.NET AJAX Toolkit CollapsiblePanel programmatically in a postback event, such as a button click. I've tried using myCollapsiblePanel.Collapsed = true but it doesn't work.
And I, being a blogger mindful of his readership, replied:
Kind and noble Kevin,
The solution to this problem is mere child's play and it certainly did *not* take me a good couple of hours wondering "WTF" while I stared in disbelief at what should be an obvious piece of code. You see, the CollapsiblePanel is "postback-aware" in that it will save its state during a postback and re-create it. This is very handy when you are submitting a form and you don't want to startle people who are easily offended by things such as UI elements that look different when a web page refreshes. That's the sort of behaviour that tends to lead to warnings on coffee cups, if you catch my meaning.
Unfortunately, this doesn't bode well if, in the postback, you want to alter its appearance manually. In this case, another line of code is necessary:
myCollapsiblePanel.ClientState = "true";
myCollapsiblePanel.Collapsed = true;
So you see, a simple solution for a simple problem (i.e. not a problem that appears simple but keeps you up all &*%$ night). Until next time, code strong and AJAX well.
Kyle the (De)Mentor