Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
it may feel stupid question but sorry to ask


i have main div like it is already closed

C#
<div id = maindiv >
</div>


now from code behind i want place another div inside the main div using the id of the main div

it should look like this

C#
<div id= maindiv>
    
    <div id=childdiv >
    </div>
<div>



can we achieve this

from code behind
Posted

Make your div element as a server control like
HTML
<div runat="server" id="mainDiv">


Then in the code behind find the div control with the id
and then add the new id element as a child the parent div using new HtmlGenericControl("div")
 
Share this answer
 
v2

  1. Give the maindiv the attribute runat="server":
    HTML
    <div id="maindiv" runat="server">

  2. Add the following using statement to your code behind:
    C#
    using System.Web.UI.HtmlControls;

  3. In your Page_Load method, add the child div:
    C#
    HtmlGenericControl div = new HtmlGenericControl("div");
    div.ID = "childdiv";
    maindiv.Controls.Add(div);

 
Share this answer
 
v2
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:

XML
<div id="maindiv " runat="server">
....
....
</div>

Then after this you can add as many divs as you like from the code behind. E.g.:
C#
string childdiv  = string.Empty;
for(int i=0; i< 5; i++)
{
   childdiv += "<div id=\"div" + i.ToString() + "\"></div>";
}
//and finally add the string to the main div's innerHTML as

maindiv.innerHTML = childdiv ;
 
Share this answer
 
Comments
murkalkiran 18-Feb-15 6:21am    
the main div is also created from code behind
like
sb.append("<div id = maindiv >
sb.append("</div>)">
[no name] 18-Feb-15 6:25am    
k,but here i put child div creation code inside the main div
murkalkiran 18-Feb-15 6:33am    
how can we access main div when it is created as string
like sb.append
[no name] 18-Feb-15 6:36am    
put runat server in ur main div then v can access the in codebehind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900