如何从代码隐藏文件中将HTML标记添加到aspx文件?
当我创建新对象
时Graph MyChart = new Graph();
我希望为此对象添加标记
<Graph id="MyChart" runat="server" Height="500px"></Graph>
解决方法是什么?
答案 0 :(得分:4)
我不确定我们是否正在讨论.NET控件或HTML,我举两个例子。
这会将其添加到页面末尾,但我建议您使用PlaceHolder
来控制添加位置:
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
示例:
<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);
}