我有一个我编写的服务器控件,通常工作正常。但是,当我添加突出显示的行时,它添加的不是一个而是两个 <br />
元素,这不是我所追求的。
mounting=new DropDownLabel();
mounting.ID="mountTypeList";
mounting.Attributes.Add("class", "mounting");
mounting.Values=Configuration.MountTypes.GetConfiguration().Options;
mounting.Enabled=Utilities.UserType == UserType.Admin;
mounting.Value=value.Reference;
td1.Controls.Add(mounting);
**td1.Controls.Add(new HtmlGenericControl("br"));**
var span=new HtmlGenericControl("span");
span.Attributes.Add("class", "mountDescription");
span.ID="mountDescription";
td1.Controls.Add(span);
对我做错了什么有什么想法吗?
预计到达时间:
我已经通过使用 jquery 添加 br 解决了这个问题,无论如何我都在那里使用它。但我看到的行为肯定是错误的。如果我添加一个元素,它应该添加该元素,而不是该元素的两倍。
最佳答案
HtmlGenericControl
将生成带有开始和结束标签的 <br>
和</br>
您可以使用 new LiteralControl("<br/>")
这应该做你想要的。
编辑
要解决这个问题,您需要自己实现 HtmlGenericControl
并将其扩展到没有关联的开始和结束标记的情况。
public class HtmlGenericSelfClosing : HtmlGenericControl
{
public HtmlGenericSelfClosing()
: base()
{
}
public HtmlGenericSelfClosing(string tag)
: base(tag)
{
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(HtmlTextWriter.TagLeftChar + this.TagName);
Attributes.Render(writer);
writer.Write(HtmlTextWriter.SelfClosingTagEnd);
}
public override ControlCollection Controls
{
get { throw new Exception("Self-closing tag cannot have child controls"); }
}
public override string InnerHtml
{
get { return String.Empty; }
set { throw new Exception("Self-closing tag cannot have inner content"); }
}
public override string InnerText
{
get { return String.Empty; }
set { throw new Exception("Self-closing tag cannot have inner content"); }
}
}
关于asp.net - 服务器控制行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7526985/