复合控件中的面板忽略可见性
我正在构建一个复合控件,它根据指定的状态在页面上呈现 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);
无论初始化期间的可见性检查如何,这基本上仍然显示面板。我测试了各种不同的场景,由于某种原因,这个场景只是忽略了状态。有什么想法吗?
谢谢,
埃里克
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Visible 标志确定控件是否在服务器上呈现。因此,当您在 CreateChildControls 期间添加控件时,ASP 将检查 Visible 标志并在 Render() 期间跳过该控件。然而,当您调用 RenderBeginTag 时,您实际上忽略了 Visible 标志。
如果您想将控件 HTML 呈现给客户端,但将 div 隐藏起来,那么您应该将 display CSS 属性设置为 none。
例如
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.