如何以编程方式将属性添加到ASP.NET页面对象中的HTML标记

Sla*_*ppy 4 asp.net

我需要在ASP.NET页面对象的标记中添加一些属性(http://www.w3.org/International/questions/qa-http-and-lang).注意:我不能以声明方式执行此操作,并且必须使用服务器端对象模型来执行此操作.有任何想法吗?

添加一些其他信息:我需要在ASP.NET页面渲染生命周期中执行此操作.我需要将属性添加到页面中的根元素.

Jus*_*tin 5

您应该看到以下MSDN文章:

看起来很简单:

myButton.Attributes.Add("myattribute", "myValue");
Run Code Online (Sandbox Code Playgroud)

更新:您可以对具有id且设置为的任何元素执行此操作runat="server",例如:

<html id="htmlTag" runat="server" ...

this.htmlTag.Attributes.Add("myAttribute", "myValue");
Run Code Online (Sandbox Code Playgroud)

  • @Slappy将`runat ="server"添加到HTML标记并给它一个id - 这应该可以解决问题. (2认同)