防止包裹 ASP.NET 服务器控件的标签
我正在编写各种 ASP.NET Server 控件,并且需要删除默认情况下包装我的控件的标签。我知道您可以将标签更改为不同的标签(如在这个问题中,如何更改自定义控件的渲染行为,使其不再是跨度),但是如何防止它发生呢?
我继承自WebControl(也可以继承自CompositeControl)。
我通常会得到:
<span>Control output</span>
我需要:
Control output
我正在重写 RenderContents(HtmlTextWriter 输出) 和 CreateChildControls() 方法(跨各种控件)。我的迫切需要是使用 RenderContents(HtmlTextWriter output) 方法解决该问题。
I am writing various ASP.NET Server controls and am needing to remove the tags that wrap my control by default. 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).
I typically get:
<span>Control output</span>
I need:
Control output
I am overriding RenderContents(HtmlTextWriter output) and the CreateChildControls() methods (across various controls). My immediate need is to address the issue using the RenderContents(HtmlTextWriter output) method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这又如何呢?
What about this?
一种更优雅的方法是使用 WebControl 的委托者(默认情况下使用 HtmlTextWriterTag.Span 调用)
并重写 RenderBeginTag 方法以添加自定义属性或其他内容:
A more elegant way to do this is by using the contrustor of WebControl (By default this is called with the HtmlTextWriterTag.Span)
and override the RenderBeginTag method to add custom attributes or other stuff:
我遇到了同样的问题。就我而言,我重写了这些方法:
为了
防止这种情况,我只需将 RenderContents 重写替换为以下内容:
希望这会有所帮助。
I was experiencing the same issue. In my case I was overriding the methods:
and
To prevent this, I simply replaced the
RenderContents
override with the following:Hope this helps.
我不认为接受的答案是完全必要的。我可能是错的,但渲染方法调用所有三个:
所以你应该能够覆盖渲染并手动调用 RenderContents:
任何人?也许我错过了一些东西。我知道这个线程很旧,但我最近遇到了这个问题,上面是我的解决方案。
I don't think the accepted answer is entirely necessary. I could be wrong, but the render method calls all three:
So you should be able to just override render and manually call RenderContents:
Anyone? Maybe I'm missing something. I know this thread is old but I ran into this recently that the above was my solution.