asp.net programmatically add multiple divs inside another div

I have a div on a user control named "placeholder".  How can I programmatically add divs inside this div to make this output.

<div id="placeholder">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
jackjohnson44Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jonjojrCommented:
What you can do is if you want to do it server side. Add runat="server" on the primary div placeholder. Then do this...

jackjohnson44Author Commented:
Thanks, but there is no code after do this....
jonjojrCommented:
I see that! sorry I'll post again I tried posting it from my phone. LOL
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

jonjojrCommented:
What you can do is if you want to do it server side. Add runat="server" on the primary div placeholder. Then do this...


in code behind:

Literal myDivs = new Literal();

myDivs.text = "<div1>value</div>";

placeholder.Controls.Add(myDivs);

Open in new window


you can do all kinds of thing with the literal by creating an array to display the valus that you want and also a datatable with the values. But the idea is that in code behind you can add that with in the div "placeholder"

Hope that helps
Kaushal AroraTechnical AnalystCommented:
If you want to add the divs from Code behind then first of all you need to add runat attribute in the main div tag like this:
<div id="placeholder" runat="server">
....
....
</div>
Then after this you can add as many divs as you like from the code behind. E.g.:
string strDivs = string.Empty;
for(int i=0; i< 5; i++)
{
   strDivs += "<div id=\"div" + i.ToString() + "\"></div>";
}
//and finally add the string to the main div's innerHTML as

placeholder.innerHTML = strDivs;

Hope it helps you.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddayx10Commented:
<div id="placeholder" runat="server"></div>

**Page Load***
//create new divs
HtmlGenericControl div1 = new HtmlGenericControl("div");
HtmlGenericControl div2 = new HtmlGenericControl("div");
HtmlGenericControl div3 = new HtmlGenericControl("div");

//add the divs to your existing div
placeholder.controls.add(div1);
placeholder.controls.add(div2);
placeholder.controls.add(div3);

There is nothing stopping you from adding other controls inside each/any of these divs, or adding them inside one another.

Ex.
div2.controls.add(div3);
div1.controls.add(div2);
placeholder.controls.add(div1);
HtmlGenericControl span = new HtmlGenericControl("span");
span.innerHTML = "Hello, I'm inside 4 nested divs";
div3.controls.add(span);

I find this method much easier to deal with in loops, with data and in manipulating controls than trying to build strings. Its a preference though. I still have developer friends who will not let go of their darn stringbuilder :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.