使用for循环或appendchild属性从后面的代码动态创建div内的多个div? [英] Dynamically create multiple divs inside a div from code behind using for loops or appendchild property?

查看:388
本文介绍了使用for循环或appendchild属性从后面的代码动态创建div内的多个div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个id为div1的div。我需要在div1中使用for循环或附加子属性添加多个div。请找到问题的解决方案



我尝试过:



I have created a div with id div1. I need to add multiple divs inside div1 using for loop or append child property.Pls find a solution to the problem

What I have tried:

ContentPlaceHolder cph;
            cph = (ContentPlaceHolder)Master.FindControl("MasterContentPlaceHolder");
            if(cph != null)
            {
                HtmlGenericControl div1 = new HtmlGenericControl("div");
                div1.ID = "div1";
                div1.Attributes["class"] = "project-list";
                cph.Controls.Add(div1);                             
            }

推荐答案

ContentPlaceHolder cph;

cph =(ContentPlaceHolder)Master.FindControl( MasterContentPlaceHolder);

if(cph!= null)

{

HtmlGenericControl div1 = new HtmlGenericControl(div);

div1.ID =div1;

div1.Attributes [class] =project-list;

cph.Controls.Add( DIV1);

}

int numberOfDiv = 10;

for(int index = 0; index< 10; index ++){

HtmlGenericControl div + index = new HtmlGenericControl(div);

(div + index).ID =div+ index;

(div + index) .Attributes [class] =project-list+ index;

cph.Controls.Add(div + index);

}



我想这应该可以解决!
ContentPlaceHolder cph;
cph = (ContentPlaceHolder)Master.FindControl("MasterContentPlaceHolder");
if(cph != null)
{
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.ID = "div1";
div1.Attributes["class"] = "project-list";
cph.Controls.Add(div1);
}
int numberOfDiv = 10;
for(int index=0;index<10;index++) {
HtmlGenericControl div+index = new HtmlGenericControl("div");
(div+index).ID = "div"+index;
(div+index).Attributes["class"] = "project-list"+index;
cph.Controls.Add(div+index);
}

I guess this should work out!


这篇关于使用for循环或appendchild属性从后面的代码动态创建div内的多个div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆