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(including 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 HtmlGenericControl object to get the "<body>" tag of the page and
then add a "div" tag as a HtmlGenericControl. For example:
Control ctrl = this.FindControl("myBody");
HtmlControl ctrlDiv = new HtmlGenericControl("div");
ctrlDiv.Attributes.Add("id","myDiv");
ctrl.Controls.Add(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.HtmlControls.HtmlForm frmMain = new HtmlForm();
frmMain.Attributes.Add("id","Form1");
frmMain.Attributes.Add("runat","server");
ctrlDiv.Controls.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="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body id="myBody" runat="server"></body>
</HTML>
----------------------------------code-behind page class
code------------------
public class DivAdd : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Control ctrl = this.FindControl("myBody");
HtmlControl ctrlDiv = new HtmlGenericControl("div");
ctrlDiv.Attributes.Add("id","myDiv");
ctrlDiv.Attributes.Add("style","POSITION: absolute; BACKGROUND-COLOR:
#66ffff");
System.Web.UI.HtmlControls.HtmlForm frmMain = new HtmlForm();
frmMain.Attributes.Add("id","Form1");
frmMain.Attributes.Add("runat","server");
Table tb = new Table();
TableRow tr = new TableRow();
TableCell cellTemp = new TableCell();
Label lblTemp = new Label();
lblTemp.Text = "Name:";
cellTemp.Controls.Add(lblTemp);
tr.Cells.Add(cellTemp);
cellTemp = new TableCell();
TextBox txtTemp = new TextBox();
txtTemp.ID = "txtName";
cellTemp.Controls.Add(txtTemp);
tr.Cells.Add(cellTemp);
tb.Rows.Add(tr);
frmMain.Controls.Add(tb);
ctrlDiv.Controls.Add(frmMain);
ctrl.Controls.Add(ctrlDiv);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(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.)