seekerOfKnowledge
seekerOfKnowledge

Reputation: 524

How do you add HTML headings dynamically in ASP.NET?

What is the server side control used to add an H4 to the markup from the code behind?

Upvotes: 1

Views: 3229

Answers (4)

James McCormack
James McCormack

Reputation: 9954

I recommend creating an HtmlGenericControl in your code-behind. The benefit of these over Literals is that they are proper HtmlControls, with the ability for you to programmatically set and modify properties such as InnerHtml, CssClass, Style etc.

HtmlGenericControl myH4 = new HtmlGenericControl("h4") 
{ 
   ID = "myH4",
   InnerHtml = "Your Heading Here" 
});
yourContainerControl.Controls.Add(myH4);

Upvotes: 2

Kon
Kon

Reputation: 27441

There is nothing like a <asp:H4 /> control. However, you can add any HTML element to a page via HtmlGenericControl type in your code behind.

For example, to create it:

HtmlGenericControl headerControl = new HtmlGenericControl(HtmlTextWriterTag.H4.ToString());
headerControl.ID = "myHeader";
headerControl.InnerHtml = "Hello World";
placeHolder.Controls.Add(headerControl);

To access it from code behind later:

HtmlGenericControl headerControl = FindControl("myHeader") as HtmlGenericControl;

Upvotes: 1

wonkim00
wonkim00

Reputation: 657

var h4 = new HtmlGenericControl("h4");
h4.InnerHtml = "Heading Text";
parentControl.Controls.Add(h4);

Upvotes: 3

Brian Beckett
Brian Beckett

Reputation: 4900

You could use an asp:Literal control - this just writes out the exact text that you set it to.

E.g:

Dim myLiteral as Literal = new Literal()
myLiteral.Text = "<h4>My Heading</h4>"

Then add your Literal to the page.

Upvotes: 1

Related Questions