How do you add HTML headings dynamically in ASP.NET?
What is the server side control used to add a开发者_运维百科n H4 to the markup from the code behind?
var h4 = new HtmlGenericControl("h4");
h4.InnerHtml = "Heading Text";
parentControl.Controls.Add(h4);
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);
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.
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;
精彩评论