简体   繁体   中英

How to Add Text Dynamically in Head Section in asp.net

I want to add the server name in my page head section dynamically like

<head>
<!-- DP2-WEB005 -->
</head>

can anyone please let me know how can I add this <!-- DP2-WEB005 --> tag in head section.

server name I will handle it but I don't know how add that commented tag dynamically.

HtmlGenericControl newControl = new HtmlGenericControl("someTag");
newControl.Attributes["someAttr"] = "some value";
Page.Header.Controls.Add(newControl);

I hope this helps ... ( the reference )

UPDATE:

This is that you want :

string serverName = "QWERTY123";
this.Page.Header.Controls.Add(new LiteralControl("<!-- " + serverName + "-->"));

And here is the output :

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title><!-- QWERTY123--></head>
<head runat="server">
<%= serverName %>
</head>

In code behind

public string serverName{get;set;}

protected void Page_Load(object o, EventArgs e)
{
 serverName="assign";
}

Try like this

Aspx:

   <html>
    <head runat="server">
      <%= Content %>
    </head>
    <body>
      //Code here
    </body>
   </html>

Code Behind:

In Code behind write the following code in PageLoad()

public string Content{get;set;}
protected void Page_Load(object sender, EventArgs e)
{  
  String Content = "Content here";
}

If you want to add css or java-script in your page section you can use the following

 var myHtmlLink = new HtmlLink { Href = @"filepath" };
 myHtmlLink.Attributes.Add("rel", "stylesheet");
 myHtmlLink.Attributes.Add("type", "text/css");
 Page.Header.Controls.AddAt("0", myHtmlLink);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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