[英]Prevent wrapping <span> tags for ASP.NET server control
I am writing various ASP.NET Server controls and am needing to remove the tags that wrap my control by default. 我正在编写各种ASP.NET Server控件,我需要删除默认情况下包装我的控件的标记。 I am aware that you can change the tag to a different tag (as in this question, How Do I Change the render behavior of my custom control from being a span ) but how can you prevent it? 我知道您可以将标记更改为其他标记(如此问题, 如何更改自定义控件的渲染行为是否为跨度 )但是如何防止它?
I am inheriting from WebControl (can also inherit from CompositeControl). 我继承自WebControl(也可以从CompositeControl继承)。
I typically get: 我通常得到:
<span>Control output</span>
I need: 我需要:
Control output
I am overriding RenderContents(HtmlTextWriter output) and the CreateChildControls() methods (across various controls). 我重写了RenderContents(HtmlTextWriter输出)和CreateChildControls()方法(跨越各种控件)。 My immediate need is to address the issue using the RenderContents(HtmlTextWriter output) method. 我迫切需要使用RenderContents(HtmlTextWriter输出)方法来解决这个问题。
What about this? 那这个呢?
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.Write("");
}
public override void RenderEndTag(HtmlTextWriter writer)
{
writer.Write("");
}
A more elegant way to do this is by using the contrustor of WebControl (By default this is called with the HtmlTextWriterTag.Span) 更优雅的方法是使用WebControl的控件(默认情况下使用HtmlTextWriterTag.Span调用)
public MyWebControl() : base(HtmlTextWriterTag.Div){}
and override the RenderBeginTag method to add custom attributes or other stuff: 并覆盖RenderBeginTag方法以添加自定义属性或其他内容:
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.AddAttribute("class", "SomeClassName");
base.RenderBeginTag(writer);
}
I was experiencing the same issue. 我遇到了同样的问题。 In my case I was overriding the methods: 在我的情况下,我重写了方法:
protected override void OnPreRender(EventArgs e)
{ /* Insert the control's stylesheet on the page */ }
and 和
protected override void RenderContents(HtmlTextWriter output)
{ /* Control rendering here, <span> tag will show up */ }
To prevent this, I simply replaced the RenderContents
override with the following: 为了防止这种情况,我只需使用以下内容替换RenderContents
覆盖:
protected override void Render(HtmlTextWriter output)
{ /* Control rendering, no <span> tag */ }
Hope this helps. 希望这可以帮助。
I don't think the accepted answer is entirely necessary. 我不认为接受的答案是完全必要的。 I could be wrong, but the render method calls all three: 我可能是错的,但render方法调用所有三个:
So you should be able to just override render and manually call RenderContents: 所以你应该能够覆盖渲染并手动调用RenderContents:
protected override void Render(HtmlTextWriter writer)
{
this.RenderContents(writer);
}
Anyone? 任何人? Maybe I'm missing something. 也许我错过了什么。 I know this thread is old but I ran into this recently that the above was my solution. 我知道这个帖子已经老了,但我最近碰到了这个,上面是我的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.