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



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

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

但是,当我在Render方法中包装RenderBeginTag(writer)RenderEndTag(writer)时,它似乎在初始化期间忽略了"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);

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

谢谢,

Eric

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

如果您想将控件HTML呈现给客户端,但保持div隐藏,那么您应该将display CSS属性设置为none。

例如

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

最新更新