Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically create a HTML comment in ASP.NET

Tags:

html

asp.net

Consider this snippet:

<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]—>

I have access to the Page object. So I can do something like this:

Page.Header.Controls.Add(new HtmlGenericControl())

How am I able to insert the above snippet to the ASP.NET Page's Header or Body?

like image 378
Sandro Avatar asked Feb 03 '26 11:02

Sandro


1 Answers

This seems hacky and there's probably a nicer solution, but this seems to work:

protected void Page_Load(object sender, EventArgs e)
{
    Literal comment = new Literal();
    comment.Text = @"<!--[if lt IE 9]><script src='//html5shiv.googlecode.com/svn/trunk/html5.js'></script><![endif]-->";
    Page.Header.Controls.Add(comment);
}
like image 68
Alistair Findlay Avatar answered Feb 05 '26 01:02

Alistair Findlay