复合控制中的面板忽略可见性

时间:2011-06-27 09:09:15

标签: .net composite-controls

我正在构建一个复合控件,它根据指定的状态在页面上呈现HTML。

如果我设置了控件,并将其添加到复合的ControlCollection,并在设置为false期间将控件的可见性设置为似乎工作正常,则会隐藏面板直到页面上的回发导致显示面板。

但是,当我在RenderBeginTag(writer)方法中包装RenderEndTag(writer)Render时,似乎在初始化期间忽略了“visible = false”语句?

// 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);

无论初始化期间的可见性检查如何,这基本上仍然显示面板。我测试了各种不同的场景,由于某种原因,这个只是忽略了状态。有什么想法吗?

谢谢,

埃里克

1 个答案:

答案 0 :(得分:2)

Visible标志确定控件是否在服务器上呈现。因此,当您在CreateChildControls期间添加控件时,ASP将检查Visible标志并在Render()期间跳过控件。但是,当您调用RenderBeginTag时,实际上忽略了Visible标志。

如果要将控件HTML呈现给客户端但保持div隐藏,则应将显示CSS属性设置为none。

e.g。

this._contentPanel.ID = "ContentPanel";
this._contentPanel.Visible = false;
this._contentPanel.Style["display"] = "none";