ASP.NET Web 表单 HTML 标记中的自定义属性 [英] Custom attributes in ASP.NET web forms HTML tag

查看:28
本文介绍了ASP.NET Web 表单 HTML 标记中的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET 3.5 框架上使用 ASP.NET 网络表单.如何在 HTML 标记中实现自定义属性,例如:

I am using ASP.NET webforms on the .NET 3.5 framework. How can I achieve a custom attribute in the HTML tag such as:

<HTML lang="en">

我想在通用继承基页上的代码中实现这一点.每次加载页面时,都会根据会话值动态设置属性值.

I want to achieve this in the code behind on a common inherited base page. The attribute value would dynamically set based on a session value everytime a page is loaded.

后期补充:如果可能的话,我想在没有对脚本标签进行任何 ASP 页面更改的情况下实现这一点

Late addition: I want to achieve this without any ASP page changes to script tags if possible

推荐答案

建议的解决方案:

<HTML lang="<%= PageLanguage %>">

工作正常.Aleris 有另一种选择,但还没有完全正确.如果将 runat="server" 属性添加到 HTML 标记,它将被解析为服务器端 HtmlGenericControl 并在 Controls 集合中可用.此外,如果添加了 id 属性,则后面的代码中将有一个变量可以直接访问它,因此:

works fine. There's another alternative that Aleris is onto but hasn't got quite right. If you add the runat="server" attribute to the HTML tag, it will be parsed to a server-side HtmlGenericControl and be available in the Controls collection. Further, if you add an id attribute, you will have a variable in the code behind to access it directly, thus:

<html runat="server" id="html">

在代码隐藏中:

html.Attributes["lang"] = "en";

注意:这适用于页面中的任何 HTML 标记.

Note: this is true for any HTML tag in your page.

编辑:我现在看到 Aleris 做对了 - 他指的是 Controls 集合中包含 html 标记(以及文档类型和任何否则直到第一个服务器标签).当然,您可以操纵此文本,这将是(正如他所说的)黑客 - 但它只会将更改限制为代码隐藏.

Edit: I see now Aleris did get it right - he refers to 'a text' (actually, a LiteralControl) in the Controls collection that contains the html tag (along with the doctype and anything else up to the first server tag). You could manipulate this text, of course, and it would be (as he says) a hack - but it would restrict the changes to code-behind only.

这篇关于ASP.NET Web 表单 HTML 标记中的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆