Home All Groups Group Topic Archive Search About

Don't understand state in WebControls class

Author
24 Aug 2006 6:33 PM
kasterborus
I've been working on a web control class that generates a grid of
checkboxes dynamically, based upon a DB query. I've been creating them
in the Render method as needed.


CheckBox _cb = new CheckBox();
_cb.Text = "Assigned";
_cb.RenderControl(tw);

The only problem is of course any user interaction with the checkboxes
is lost when posting back.

I've tried adding them to the .Controls collection, setting
EnableViewState to true, but nothing seems to work. The closest I've
come to success is when I store the Checkboxes in the Viewstate
explictly, but they are the "previous" checkboxes - no user interaction
is recorded in them after postback.

I know this has something to do with EnsureChildControls() and
CreateChildControls() but all my attempts so far have failed.

How do I tie a checkbox control (newly created or otherwise) to the
state of the checkbox in the HTML, so that it contains any new user
interaction?

Thanks in advance!

Author
27 Aug 2006 12:15 PM
Teemu Keiski
They should be added to Controls collection in PreRender stage
(Page_PreRender method or overridden OnPreRender method), so that state gets
saved. Plus that they are dynamical controls, they would need to be
recreated on postback.

http://aspadvice.com/blogs/joteke/archive/2006/01/06/14568.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


<kasterbo***@yahoo.com> wrote in message
Show quoteHide quote
news:1156444432.679943.315250@b28g2000cwb.googlegroups.com...
> I've been working on a web control class that generates a grid of
> checkboxes dynamically, based upon a DB query. I've been creating them
> in the Render method as needed.
>
>
> CheckBox _cb = new CheckBox();
> _cb.Text = "Assigned";
> _cb.RenderControl(tw);
>
> The only problem is of course any user interaction with the checkboxes
> is lost when posting back.
>
> I've tried adding them to the .Controls collection, setting
> EnableViewState to true, but nothing seems to work. The closest I've
> come to success is when I store the Checkboxes in the Viewstate
> explictly, but they are the "previous" checkboxes - no user interaction
> is recorded in them after postback.
>
> I know this has something to do with EnsureChildControls() and
> CreateChildControls() but all my attempts so far have failed.
>
> How do I tie a checkbox control (newly created or otherwise) to the
> state of the checkbox in the HTML, so that it contains any new user
> interaction?
>
> Thanks in advance!
>