简体   繁体   English

ASP.NET web forms HTML 标记中的自定义属性

[英]Custom attributes in ASP.NET web forms HTML tag

I am using ASP.NET webforms on the .NET 3.5 framework.我在 .NET 3.5 框架上使用 ASP.NET 网络表单。 How can I achieve a custom attribute in the HTML tag such as:如何在 HTML 标记中实现自定义属性,例如:

<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.每次加载页面时,属性值将根据 session 值动态设置。

Late addition: I want to achieve this without any ASP page changes to script tags if possible后期添加:如果可能的话,我想在不对脚本标签进行任何 ASP 页面更改的情况下实现这一点

The suggested solution:建议的解决方案:

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

works fine.工作正常。 There's another alternative that Aleris is onto but hasn't got quite right. Aleris 还有另一种选择,但还没有完全正确。 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.如果将 runat="server" 属性添加到 HTML 标记,它将被解析为服务器端 HtmlGenericControl 并在 Controls 集合中可用。 Further, if you add an id attribute, you will have a variable in the code behind to access it directly, thus:此外,如果您添加一个 id 属性,您将在后面的代码中有一个变量可以直接访问它,因此:

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

in codebehind:在代码隐藏中:

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

Note: this is true for any HTML tag in your page.注意:这适用于页面中的任何 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).编辑:我现在看到 Aleris 确实做对了 - 他在包含 html 标记(以及 doctype 和第一个服务器标记之前的任何其他内容)的 Controls 集合中引用了“文本”(实际上是 LiteralControl)。 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.当然,您可以操纵此文本,这将是(正如他所说)一种黑客行为 - 但它会将更改限制为仅代码隐藏。

if you happen to use the existing ASP.NET Web Page Globalization scaffolding, then you want to use this instead:如果您碰巧使用现有的ASP.NET Web Page Globalization脚手架,那么您想改用这个:

@using System.Threading
<html lang="@Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName">

I use AddParsedSubObject method of Page class.我使用页面 class 的 AddParsedSubObject 方法。

You can get control object by AddParsedSubObject method parameter in parsing process.您可以在解析过程中通过 AddParsedSubObject 方法参数来控制 object。

Override this method such as...覆盖此方法,例如...

protected override void AddParsedSubObject(object control)
{
    if (obj is LiteralControl) 
    {
        String html = (obj as LiteralControl).Text;
        if (Regex.IsMatch(html, "<html[^>]*>") == true)
        {
            String newhtml = Regex.Replace(html, "<html[^>]*>", "<html lang=\"en\">");
            base.AddParsedSubObject(new LiteralControl(newhtml));
        }
    }
}

You can customize your output of html tag and also other tag.您可以自定义 html 标签的 output 以及其他标签。 Hope your help!!希望你的帮助!!

Question is old but may be of use to someone.问题很旧,但可能对某人有用。 Here's what I did..这就是我所做的..

In code behind:在后面的代码中:

  public string langName
            {
                get
                {
                    return Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
                }
            }

In ASP.NET PAGE在 ASP.NET 页面

<html lang="<%= langName %>">

I was working with Local Resources for each language & cultures also so I found this to be a better solution.我也在与每种语言和文化的Local Resources合作,所以我发现这是一个更好的解决方案。

Also, because I was using a Master page, I had to add it only once.另外,因为我使用的是母版页,所以我只需要添加一次。

in html:在 html 中:

<HTML lang="<%=myLang%>">

In codebehind:在代码隐藏中:

protected string myLang = "en"

Solution 1:解决方案1:

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

where PageLanguage is virtual protected property of your base page.其中 PageLanguage 是基本页面的虚拟受保护属性。 The value is overriden in the derrived pages (from what I understand you need to to change this on page level?)该值在派生页面中被覆盖(据我了解,您需要在页面级别更改此值?)

Solution 2:解决方案2:

Hack-it: the Page.Controls[0] contain a text that contains the html tag.破解:Page.Controls[0] 包含一个包含 html 标记的文本。 A simple replace on page prerender event would do it.页面预渲染事件上的简单替换就可以了。

If you're just trying to do internationalisation of your website, may as well use the in-built systems provided by .net (cause they are beautiful).如果您只是想对您的网站进行国际化,不妨使用 .net 提供的内置系统(因为它们很漂亮)。 Is that what you want to do?那是你想做的吗? Or something else?或者是其他东西?

I use this to set the language I'm using in my code:我用它来设置我在代码中使用的语言:

<html xmlns="http://www.w3.org/1999/xhtml" lang="<%= System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName %>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM