问题描述
我试图写一个简单的工具来三夏显示网页内的单行消息 - 状态更新,错误信息等的信息将来自其他控件的页面上,通过调用夏精的方法。如果控制不具备它得到pre-渲染时间的任何消息,我不希望它在页面上呈现在所有 - 我希望它设置Control.Visible = FALSE。这似乎只对非回发的渲染工作,虽然。这里的code我使用的是:
I am trying to write a simple utility webcontrol to display one-line messages inside a web page - status updates, error messages, etc. The messages will come from other controls on the page, by calling a method on the webcontrol. If the control doesn't have any messages by the time it gets to pre-render, I don't want it to render on the page at all - I want it to set Control.Visible = false. This only seems to work for non-postback rendering though. Here's the code I'm using:
public class MessageList : WebControl
{
#region inner classes
private struct MessageItem
{
string Content, CssClass;
public MessageItem(string content, string cssClass)
{
Content = content;
CssClass = cssClass;
}
public override string ToString()
{ return "<li" + (String.IsNullOrEmpty(CssClass) ? String.Empty : " class='" + CssClass + "'") + ">" + Content + "</li>"; }
}
private class MessageQueue : Queue<MessageItem> { }
#endregion
#region fields, constructors, and events
MessageQueue queue;
public MessageList() : base(HtmlTextWriterTag.Ul)
{
queue = new MessageQueue();
}
protected override void OnLoad(EventArgs e)
{
this.Controls.Clear();
base.OnLoad(e);
}
protected override void OnPreRender(EventArgs e)
{
this.Visible = (queue.Count > 0);
if (this.Visible)
{
while (queue.Count > 0)
{
MessageItem message = queue.Dequeue();
this.Controls.Add(new LiteralControl(message.ToString()));
}
}
base.OnPreRender(e);
}
#endregion
#region properties and methods
public void AddMessage(string content, string cssClass)
{ queue.Enqueue(new MessageItem(content, cssClass)); }
public void AddMessage(string content)
{ AddMessage(content, String.Empty); }
#endregion
}
我试图把里面的CreateChildControls检查过,具有相同的结果。
I tried putting the check inside CreateChildControls too, with the same result.
推荐答案
除了修改为Visible属性,试试这个:
Instead of modifying the 'Visible' property, try this :
protected override void Render(HtmlTextWriter writer)
{
if (queue.Count > 0)
base.Render(writer);
}
这篇关于在控制生命周期的哪一点Control.Visible停止渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!