I'm building a composite control that renders HTML on a page based on a specified state.
If I set up the control, and add it to the ControlCollection
for composite, and set the visibility of the control during set up to false
it seems to be working fine, the panel is hidden until a postback on the page causes the panel to be displayed.
But, when I wrap a RenderBeginTag(writer)
and RenderEndTag(writer)
in the Render
method, it seems to be ignoring the "visible = false
" statement during initialization?
// initialization
this._contentPanel = new Panel();
this._contentPanel.ID = "ContentPanel";
this._contentPanel.Visible = false;
this.Controls.Add(this._contentPanel);
// CreateChildControls
this.InitContentPanel(); // adds the content panel to control collection
// render
this._contentPanel.RenderBeginTag(writer);
writer.WriteLine("<div>Some copy here</div>");
this._contentPanel.RenderEndTag(writer);
This basically still displays the panel, regardless of the visibility check during initialization. I've tested various different scenarios, and for some reason this one just ignores the state. Any ideas?
Thanks,
Eric
The Visible flag determines whether the control is rendered on the server. Therefore when you add the control during CreateChildControls ASP will check the Visible flag and skip the control during Render(). However when you call RenderBeginTag you are effectively ignoring the Visible flag.
If you want to render the controls HTML to the client but keep it the div hidden then you should set the display CSS attribute to none.
e.g.
this._contentPanel.ID = "ContentPanel";
this._contentPanel.Visible = false;
this._contentPanel.Style["display"] = "none";