且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态添加特定标签的控件

更新时间:2023-12-05 09:48:10

 HtmlTable ht =  new  HtmlTable(); 
HtmlTableRow r1 = new HtmlTableRow();
HtmlTableCell c1 = new HtmlTableCell();
c1.InnerHtml = 名称;
r1.Cells.Add(c1);
TextBox t1 = new TextBox();
HtmlTableCell c2 = new HtmlTableCell();
c2.Controls.Add(t1);
r1.Cells.Add(c2);
ht.Rows.Add(r1);
.....................等等......可以它内循环也
Form1.Controls.Add(ht);


然后你可以添加通用控件到页面并添加控件到这个泛型像这样

 HtmlGenericControl div =  new  HtmlGenericControl( 格跨度>); 
div.ID = div3;
for int i = 0 ; i< 5; i ++)
{
TextBox t = new TextBox();
div.Controls.Add(t);
}
.Form1.Controls.Add(div);


使用asp:panel bcz面板也渲染到div元素

添加文本框如下所示

< asp:panel id =   pnltest runat =   server >  < /   asp:panel  >  

for int i = 0 ; i< 5; i ++)
{
TextBox t = new TextBox();
pnltest.Controls.Add(t);
}


Hi guys,
I want to add a control at specific tag.
for example:
I have the following html/asp tags

<h2>
        page 1</h2>
    <div id="DynFormDiv">

    </div>



Now, I want to add a control (for example textbox) at this div (DynFormDiv).
How can I achieve this?

HtmlTable ht=new HtmlTable();
HtmlTableRow r1 =new HtmlTableRow();
HtmlTableCell c1=new HtmlTableCell();
c1.InnerHtml="Name";
r1.Cells.Add(c1);
TextBox t1=new TextBox();
HtmlTableCell c2=new HtmlTableCell();
c2.Controls.Add(t1);
r1.Cells.Add(c2);
ht.Rows.Add(r1);
..................... so on... can do it inside loop also
Form1.Controls.Add(ht);


then you can add generic control to page and add control to that generic like this
HtmlGenericControl div=new HtmlGenericControl("div");
div.ID="div3";
for(int i=0;i<5;i++)
{
 TextBox t=new TextBox();
 div.Controls.Add(t);
}
this.Form1.Controls.Add(div);


use asp:panel bcz panel also renders to div element
the add textboxes to it like the following
<asp:panel id="pnltest" runat="server"></asp:panel>

for (int i=0; i<5; i++)
{
  TextBox t=new TextBox();
  pnltest.Controls.Add(t);
}