防止包裹 ASP.NET 服务器控件的标签

发布于 2024-09-25 13:41:25 字数 572 浏览 5 评论 0原文

我正在编写各种 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆光飞翔i 2024-10-02 13:41:25

这又如何呢?

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.Write("");
    }

    public override void RenderEndTag(HtmlTextWriter writer)
    {
        writer.Write("");
    }

What about this?

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.Write("");
    }

    public override void RenderEndTag(HtmlTextWriter writer)
    {
        writer.Write("");
    }
姜生凉生 2024-10-02 13:41:25

一种更优雅的方法是使用 WebControl 的委托者(默认情况下使用 HtmlTextWriterTag.Span 调用)

public MyWebControl() : base(HtmlTextWriterTag.Div){}

并重写 RenderBeginTag 方法以添加自定义属性或其他内容:

public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.AddAttribute("class", "SomeClassName");
        base.RenderBeginTag(writer);
    }

A more elegant way to do this is by using the contrustor of WebControl (By default this is called with the HtmlTextWriterTag.Span)

public MyWebControl() : base(HtmlTextWriterTag.Div){}

and override the RenderBeginTag method to add custom attributes or other stuff:

public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.AddAttribute("class", "SomeClassName");
        base.RenderBeginTag(writer);
    }
一指流沙 2024-10-02 13:41:25

我遇到了同样的问题。就我而言,我重写了这些方法:

protected override void OnPreRender(EventArgs e)
    { /* Insert the control's stylesheet on the page */ }

为了

protected override void RenderContents(HtmlTextWriter output)
        { /* Control rendering here, <span> tag will show up */ }

防止这种情况,我只需将 RenderContents 重写替换为以下内容:

protected override void Render(HtmlTextWriter output)
        { /* Control rendering, no <span> tag */ }

希望这会有所帮助。

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:

protected override void Render(HtmlTextWriter output)
        { /* Control rendering, no <span> tag */ }

Hope this helps.

書生途 2024-10-02 13:41:25

我不认为接受的答案是完全必要的。我可能是错的,但渲染方法调用所有三个:

  • RenderBeginTag
  • RenderContents
  • RenderEndTag

所以你应该能够覆盖渲染并手动调用 RenderContents:

protected override void Render(HtmlTextWriter writer)
{ 
    this.RenderContents(writer);
}

任何人?也许我错过了一些东西。我知道这个线程很旧,但我最近遇到了这个问题,上面是我的解决方案。

I don't think the accepted answer is entirely necessary. I could be wrong, but the render method calls all three:

  • RenderBeginTag
  • RenderContents
  • RenderEndTag

So you should be able to just override render and manually call 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文