Reputation: 1017
How can I add HTML tags to aspx file from the code-behind?
When I create new object
Graph MyChart = new Graph();
I want it add a tag for this object
<Graph id="MyChart" runat="server" Height="500px"></Graph>
What is the solution for that?
Upvotes: 3
Views: 8363
Reputation: 21112
Not sure if we are talking about a .NET control or HTML on the fly, I give examples of both.
This will add it to the end of the page, but I suggest you use a PlaceHolder
to control where it gets added:
Graph MyChart = new Graph();
MyChart.ID = "MyChart";
Page.Controls.Add(MyChart);
//genericcontrol example
HtmlGenericControl NewControl = new HtmlGenericControl("graph");
// Set the properties of the new HtmlGenericControl control.
NewControl.ID = "MyGraph";
Page.Controls.Add(NewControl);
PlaceHolder
example:
<form id="form1" runat="server">
<h3>PlaceHolder Example</h3>
<asp:PlaceHolder id="PlaceHolder1"
runat="server"/>
</form>
protected void Page_Load(Object sender, EventArgs e)
{
Graph MyChart = new Graph();
MyChart.ID = "MyChart";
PlaceHolder1.Controls.Add(MyChart);
//genericcontrol example
HtmlGenericControl NewControl = new HtmlGenericControl("graph");
// Set the properties of the new HtmlGenericControl control.
NewControl.ID = "MyGraph";
PlaceHolder1.Controls.Add(NewControl);
}
Upvotes: 4