Custom attributes in ASP.NET web forms HTML tag

佐手、 提交于 2019-11-28 05:09:26

问题


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.

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


回答1:


The suggested solution:

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

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

in codebehind:

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

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

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.




回答2:


I use AddParsedSubObject method of Page class.

You can get control object by AddParsedSubObject method parameter in parsing process.

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. Hope your help!!




回答3:


if you happen to use the existing ASP.NET Web Page Globalization scaffolding, then you want to use this instead:

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



回答4:


in html:

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

In codebehind:

protected string myLang = "en"



回答5:


Solution 1:

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

where PageLanguage is virtual protected property of your base page. The value is overriden in the derrived pages (from what I understand you need to to change this on page level?)

Solution 2:

Hack-it: the Page.Controls[0] contain a text that contains the html tag. A simple replace on page prerender event would do it.




回答6:


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

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

I was working with Local Resources for each language & cultures also so I found this to be a better solution.

Also, because I was using a Master page, I had to add it only once.




回答7:


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). Is that what you want to do? Or something else?




回答8:


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


来源:https://stackoverflow.com/questions/618339/custom-attributes-in-asp-net-web-forms-html-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!