Control.Visible=false affects CreateChildControls

A

Aren Cambre

I have had a vexing problem with composite custom controls and how
their CreateChildControls method is fired.

I have a composite control which contains CheckBox controls. The
composite control sits in a Panel.

I also have two standalone Button controls. Button A's Click event
handler sets Panel.Visible = false. Button B's Click event handler
attempts to read the Checked properties of the CheckBox controls in
the composite control in the Panel.

If I press button B without pressing button A, then B's Click event
handler works fine: it can read the Checked properties of the CheckBox
controls. However, if I press button B after button A (that is, read
the child controls after setting Panel.Visible = false), then button
B's Click event handler encounters the composite controls with no
child controls. Their CreateChildControls methods were not called, and
Control.HasControls() returns false.

Normally CreateChildControls is fired early enough that a button Click
event handler can access the child controls of the composite control.
However, if I set Panel.Visible = false and then post back again, the
CreateChildControls method is called very late in the page's
lifecycle, making it impossible for me to do anything with the Child
Controls even in the Page's Init and Load event handlers.

This appears to be an inconsistency within ASP.Net.

I have discovered two workarounds:

1. Instead of using Panel.Visible = false, do Panel.Style["display"] =
"none". This way the custom controls still render, but the browser
just does not display them. You are depending on an "honest" client,
however, so this may not be preferred.
2. Before you access any child controls of a composite control, have
that control call its EnsureChildControls() method. According to MS's
documentation, this will set ChildControlsCreated = true and call the
CreateChildControls method.

Aren Cambre
Southern Methodist University
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top