Custom Attributes in ASP.NET Web Form HTML Tags

I am using ASP.NET web forms in .NET 3.5. How can I get a custom attribute in an HTML tag, for example:

<HTML lang="en">

I want to achieve this in code on a common inherited base page. The attribute value will be dynamically set based on the session value each time the page loads.

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

+3
source share
8 answers

Proposed Solution:

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

. , , . runat = "server" HTML, HtmlGenericControl Controls. , id, , :

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

codebehind:

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

: HTML- .

: , Aleris - "" (, LiteralControl) Controls, html ( doctype ..) ). , , ( ) - .

+8

AddParsedSubObject Page.

AddParsedSubObject .

, ...

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));
        }
    }
}

html, . !

+2

html:

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

codebehind:

protected string myLang = "en"
+1

1:

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

PageLanguage . ( , ?)

2:

Hack-it: . Controls [0] , html. prerender .

+1

ASP.NET Web- , :

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

, - . .

:

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

ASP.NET PAGE

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

Local Resources , , .

, , .

+1

, , .net( ). , ? - ?

0

, , :

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

Source: https://habr.com/ru/post/1704323/