Hi,
Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you're dealing with something on Dynamically
constructing a page. The page doesn't have any static template contents,
you'd like to add all the controls dynamicaly in the page class's code.
Also, you want to put a "<div>" tag right inside the "<body>" tag and other
controls(includ ing the server <form> tag) all inside the "<div>" control.
If there is anything I misunderstood, please feel free to let me know.
Based on my research, in an ASP.NET web page, the controls are organized in
a large strucute. And most derived controls(both HtmlControls and ASP.NET
server controls) can use the "Controls" member collection to add sub
controls. Just add them following the right order so that they can be
constructed in the correct structure. So as for the problem in our issue, I
think we can do it as this:
1. use HtmlGenericCont rol object to get the "<body>" tag of the page and
then add a "div" tag as a HtmlGenericCont rol. For example:
Control ctrl = this.FindContro l("myBody");
HtmlControl ctrlDiv = new HtmlGenericCont rol("div");
ctrlDiv.Attribu tes.Add("id","m yDiv");
ctrl.Controls.A dd(ctrlDiv);
2. Since in ASP.NET all the server controls should be placed in a "<form>"
tag whose "runat" is set as "server". So if we use servercontrols, we need
to add a such form tag, just like:
System.Web.UI.H tmlControls.Htm lForm frmMain = new HtmlForm();
frmMain.Attribu tes.Add("id","F orm1");
frmMain.Attribu tes.Add("runat" ,"server");
ctrlDiv.Control s.Add(frmMain);
3. After that, we can place all other controls inside the server "<form>"
tag. Just create their instance and add them into the server form tag
object's Controls collection.
Also, I've build a simple page based on the above steps, you may have a try
if you think necessary.
-------------------------------aspx page
code---------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DivAdd </title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body id="myBody" runat="server"> </body>
</HTML>
----------------------------------code-behind page class
code------------------
public class DivAdd : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
Control ctrl = this.FindContro l("myBody");
HtmlControl ctrlDiv = new HtmlGenericCont rol("div");
ctrlDiv.Attribu tes.Add("id","m yDiv");
ctrlDiv.Attribu tes.Add("style" ,"POSITION: absolute; BACKGROUND-COLOR:
#66ffff");
System.Web.UI.H tmlControls.Htm lForm frmMain = new HtmlForm();
frmMain.Attribu tes.Add("id","F orm1");
frmMain.Attribu tes.Add("runat" ,"server");
Table tb = new Table();
TableRow tr = new TableRow();
TableCell cellTemp = new TableCell();
Label lblTemp = new Label();
lblTemp.Text = "Name:";
cellTemp.Contro ls.Add(lblTemp) ;
tr.Cells.Add(ce llTemp);
cellTemp = new TableCell();
TextBox txtTemp = new TextBox();
txtTemp.ID = "txtName";
cellTemp.Contro ls.Add(txtTemp) ;
tr.Cells.Add(ce llTemp);
tb.Rows.Add(tr) ;
frmMain.Control s.Add(tb);
ctrlDiv.Control s.Add(frmMain);
ctrl.Controls.A dd(ctrlDiv);
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
-------------------------------------------------------------------------
Please check out the preceding suggestion. If you have any question or need
any further help, please feel free to post here.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)