开发者

Create div tag dynamically

开发者 https://www.devze.com 2023-04-12 21:36 出处:网络
I want to create a DIV dynamically and place it into another DIV when a button is clicked. So far I\'ve managed to create a new DIV on开发者_如何学运维 button_click, however I don\'t know how to plac

I want to create a DIV dynamically and place it into another DIV when a button is clicked.

So far I've managed to create a new DIV on开发者_如何学运维 button_click, however I don't know how to place the new DIV object into a specific DIV tag as a child.

Here's my code which is within the button's click event.

   System.Web.UI.HtmlControls.HtmlGenericControl dynDiv = new    System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
   dynDiv.ID = "dynDivCode" + i.ToString();
   dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
   dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
   dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");

   dynDiv.InnerHtml = "I was created using Code Behind";

   this.Controls.Add(dynDiv);

Any suggestions please? thanks


Your specific existing div should be server control. For example: Panel. Then you can add as a child to the Controls collection of the panel.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号