ASP.NET,C#

顾名思义,我想知道是否有人知道如何编程(文件后面的C#代码)将div添加到另一个容器div(在aspx页中)。

提前致谢

最佳答案

//创建div的新实例并设置所有值,例如ID
查看简短的代码示例。它对我来说可以在网络添加中创建Divs

System.Web.UI.HtmlControls.HtmlGenericControl NewDiv = new
    System.Web.UI.HtmlControls.HtmlGenericControl();
    NewDiv.ID = "divcreated";

或者
protected void Page_Load(object sender, EventArgs e)
{
    System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
    new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
    createDiv.ID = "createDiv";
    createDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Yellow");
    createDiv.Style.Add(HtmlTextWriterStyle.Color, "Red");
    createDiv.Style.Add(HtmlTextWriterStyle.Height, "100px");
    createDiv.Style.Add(HtmlTextWriterStyle.Width, "400px");
    createDiv.InnerHtml = " I'm a div, from code behind ";
    this.Controls.Add(createDiv);
}

10-06 13:29