Write HTML markup from code in asp.net

If the name is confused, suggest changing it

What is the best way to write your own HTML?

I am currently using this:

<asp:Literal ID="ltr" runat="server"></asp:Literal> 

and from the code behind:

 ltr.Text = "<p class=\"specific-class\"></p>"; 

Is it possible to do something like this?

Is there a better way to do this?

+6
source share
2 answers

You can do it like this, but please do not do it. For the sake of your fellow developers in the past, present and future, separate your code from your design. Now, if you have to write markup in the code behind, this is definitely a way to do it.

However, if all you need to do is add a literal / span / textbox with a specific class, you can do something like this:

(Given that you have a panel with runat="server" called "myPanel")

 myPanel.Controls.Add(new Label { Text = "Hello!", CssClass = "specific-class" }); 
+6
source share

You can try using HtmlGenericControl. You can specify the tag name of the html element. You can learn more about this here: http://msdn.microsoft.com/en-us/library/7512d0d0 (v = vs .71) .aspx

+3
source share

All Articles