在ASP.NET Web窗体的HTML标记的自定义属性 [英] Custom attributes in ASP.NET web forms HTML tag

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

问题描述

我使用的.NET 3.5框架ASP.NET web表单。我怎样才能实现在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">

我想在后面一个常见的​​遗传基页上的code键实现这一点。该属性值将动态设置的基础上每次页面加载会话值。

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 %>">

工作正常。还有另外一个替代方案,爱励是上,但还没有得到完全正确。如果添加=服务器属性的HTML标记,它会被解析到一个服务器端HtmlGenericControl,并在Controls集合可用。此外,如果添加一个id属性,你将在code变量后面直接访问它,因此:

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">

在codebehind:

in codebehind:

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

请注意:这是真的在你的页面的任何HTML标记

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

修改:我现在看到爱励没有得到它的权利 - 他指的是包含HTML标记(连同DOCTYPE和任何控件集合创文(实际上,一个LiteralControl)否则到第一服务器标记)。你可以操纵这个文本,当然,这将是(他说)一劈 - 但它会限制更改为code-仅次于

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天全站免登陆