ASP.NET:如何以声明方式向 Web 控件添加自定义/命名空间 XHTML 属性?

发布于 2024-09-16 00:38:28 字数 497 浏览 3 评论 0原文

通常,Web 控件的未知属性会传递到浏览器中呈现的元素。所以下面的作品。

<asp:label runat="server" Text="Label Text" helpId="101" />

但是,如果您使用如下所示的命名空间属性

<asp:label runat="server" Text="Label Text" myNs:helpId="101" /></div>

,即使在 html 元素中声明了自定义命名空间,该属性也不会呈现给客户端:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:myNs="http://www.acme.com/htmlext">

有谁知道一种方法可以使其呈现给客户端,而无需使用自定义控件。模块或其他全局“可插入”解决方案是可以接受的。

Normally, unknown attributes of a webcontrol are passed through to to the rendered element in the browser. So the following works.

<asp:label runat="server" Text="Label Text" helpId="101" />

However, if you use a namespaced attribute like the following

<asp:label runat="server" Text="Label Text" myNs:helpId="101" /></div>

The attribute is not rendered to the client, even when the custom namespace is declared in the html element like:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:myNs="http://www.acme.com/htmlext">

Does anyone know of a way to get this to render to the client, without having to use a custom control. A module or other globally "pluggable" solution would be acceptable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

俏︾媚 2024-09-23 00:38:28

找到这篇文章 MSDN...看起来很有希望。
但您需要创建一个自定义 Web 控件。

WebControl.AddAttributesToRender 方法

将需要呈现的 HTML 属性和样式添加到指定的 HtmlTextWriterTag。

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
  protected override void AddAttributesToRender(HtmlTextWriter writer) 
  {

     writer.AddAttribute("myNs:helpId", "101");
     base.AddAttributesToRender(writer);

  }

Found this article on MSDN... looks promising.
But you will need to create a custom webcontrol.

WebControl.AddAttributesToRender Method

Adds HTML attributes and styles that need to be rendered to the specified HtmlTextWriterTag.

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
  protected override void AddAttributesToRender(HtmlTextWriter writer) 
  {

     writer.AddAttribute("myNs:helpId", "101");
     base.AddAttributesToRender(writer);

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