Add <div> to Body?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to wrap all of the HTML inside of my <body> tag inside a <div> with in my code-behind. I do not want to touch the .aspx page template at all. I know how to make the body tag runat=server and give it an id so I can find it from the code-behind, but after that I want to add the <div>. One problem I see is that I just add the <div> to the <body>, all of the other controls (including the aspx <form>) will not be inside it.

What can I do?

Thanks.
 
I think the panel control wraps the <div> tag. Counldn't you add a panel
control and then add all your webcontrols to the panels control collection.



localhost said:
I would like to wrap all of the HTML inside of my <body> tag inside a
<div> with in my code-behind. I do not want to touch the .aspx page
template at all. I know how to make the body tag runat=server and give it
an id so I can find it from the code-behind, but after that I want to add
 
I'm not following you. If you want a div tag in your Page, just put one
there in the Page Template (.sapx). You can put any HTML you want into the
Page Template. And, according to the rules of HTML, and tag or element that
is between the <div> and </div> tags will be inside the div, including form
and all other Controls that render HTML objects in the page.

Offhand, it sounds like you're trying to do something oddly here. What is
the requirement that this div is supposed to fulfill?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

localhost said:
I would like to wrap all of the HTML inside of my <body> tag inside a
<div> with in my code-behind. I do not want to touch the .aspx page
template at all. I know how to make the body tag runat=server and give it
an id so I can find it from the code-behind, but after that I want to add
 
I am absolutely not looking to touch the page template at all. I am using program code only to build a page. I don't want to put any HTML in the page template at all because I have custom classes that make modifications to many pages at runtime, and I don't want to hand-edit 223 pages with <div> tags as I go, either. :)

I know that I want to place a the <div> inside the <body>, but then I want to put everything else inside that <div>. That will let me use CSS to define how the page flows overall, letting me handle silly user window maximizes and etc

The following almost works

I make my <div> (it's an HtmlGenericControl) called myDiv.
Then I do a Controls.AddAt(pageBody.Controls.Count,myDiv).
Then I do a myDiv.Controls.Add(myForm).
Then I do a pageBody.Controls.Remove(myForm)

For some reason my form controls are now displaying outside of the <div> style area I defined, but that might be a (mis)function of my CSS code. Hmmm..




I'm not following you. If you want a div tag in your Page, just put on
there in the Page Template (.sapx). You can put any HTML you want into th
Page Template. And, according to the rules of HTML, and tag or element tha
is between the <div> and </div> tags will be inside the div, including for
and all other Controls that render HTML objects in the page

Offhand, it sounds like you're trying to do something oddly here. What i
the requirement that this div is supposed to fulfill

--
HTH
Kevin Spence
...Net Develope
Microsoft MV
Big things are made u
of lots of little things

localhost said:
I would like to wrap all of the HTML inside of my <body> tag inside
<div> with in my code-behind. I do not want to touch the .aspx pag
template at all. I know how to make the body tag runat=server and give i
an id so I can find it from the code-behind, but after that I want to ad
 
I can't imagine what sort of CSS you would use on a div which encompasses
everything in the page. You might as well apply a class to the <body> of the
page if that's what you're after. I can certainly agree with and appreciate
the use of divs in the page with classes on each one so that you can use CSS
to position them and work with their styles, but the whole page? And if you
use divs (or ASP.Net Panels) for your content, you dont' need to do anything
outside of the WebForm, as that should encompass the entire contents of the
viewable page.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

localhost said:
I am absolutely not looking to touch the page template at all. I am using
program code only to build a page. I don't want to put any HTML in the page
template at all because I have custom classes that make modifications to
many pages at runtime said:
I know that I want to place a the <div> inside the <body>, but then I want
to put everything else inside that <div>. That will let me use CSS to
define how the page flows overall, letting me handle silly user window
maximizes and etc.
The following almost works:

I make my <div> (it's an HtmlGenericControl) called myDiv.
Then I do a Controls.AddAt(pageBody.Controls.Count,myDiv).
Then I do a myDiv.Controls.Add(myForm).
Then I do a pageBody.Controls.Remove(myForm).

For some reason my form controls are now displaying outside of the <div>
style area I defined, but that might be a (mis)function of my CSS code.
Hmmm...
 
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.)
 
I agree with Kevin's point - the div doesn't give you any advantage from a
CSS standpoint because anything you do with a div you can do with a body
element. Instead of:
div.mydivclass a { }
use
body a { }

and so on.

You code for adding and removing the form element seemed fine. I just
suggest you do it in a loop - loop over the page's Controls collection and
add and remove them one by one.

Eran
 
You are right. When I said "everything" I should have written "everything .NET". There will be other contributing pieces of code that need to get placed in the body, and I don't want them to act the same as all my .NET stuff. For example, I am working on a menu system with its own <form> stuff and I don't want that CSS to get mangled by my "main" CSS.

Thanks to Kevin and Eran and Steven Cheng
 
You know, it sounds almost as if you'd be better off discarding the ASP.Net
Page class altogether and wiritng your own custom HttpHandler. Could be a
lot of work, though.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

localhost said:
You are right. When I said "everything" I should have written "everything
..NET". There will be other contributing pieces of code that need to get
placed in the body, and I don't want them to act the same as all my .NET
 
Back
Top