473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Add <div> to Body?

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.
Nov 18 '05 #1
8 4828
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" <pr*******@cohort.ces> wrote in message
news:6D**********************************@microsof t.com...
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.

Nov 18 '05 #2
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" <pr*******@cohort.ces> wrote in message
news:6D**********************************@microsof t.com...
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.

Nov 18 '05 #3

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" <pr*******@cohort.ces> wrote in messag
news:6D**********************************@microsof t.com..
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
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


Nov 18 '05 #4
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" <pr*******@cohort.ces> wrote in message
news:2D**********************************@microsof t.com...

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 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" <pr*******@cohort.ces> wrote in message
news:6D**********************************@microsof t.com...
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.

Nov 18 '05 #5
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.)

Nov 18 '05 #6
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
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
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" <pr*******@cohort.ces> wrote in message
news:2D**********************************@microsof t.com...

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 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" <pr*******@cohort.ces> wrote in message
news:6D**********************************@microsof t.com...
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.


Nov 18 '05 #7

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
I can't imagine what sort of CSS you would use on a div which
encompasses everything in the page.

Nov 18 '05 #8
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" <pr*******@cohort.ces> wrote in message
news:18**********************************@microsof t.com...

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.

I can't imagine what sort of CSS you would use on a div which
encompasses everything in the page.

Nov 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jens Kristensen | last post by:
I have a problem displaying a divbox containing a html-textarea - everything works fine with "normal" characters. However, when the textarea contains special chars like <P> or ' , the box fails to...
2
by: listaction | last post by:
Hi Folks, can you help me by explaining how the code below can be translated using <div> and achieve the same effect? Thanks, LA <html> <body> <table bgcolor="#000000" width="100%"...
5
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of...
9
by: Julia Briggs | last post by:
How do I construct a <iframe> or equivalent for FireFox/NS browsers, inside a screen centered <div> tag? Can it be done?
5
by: Vear | last post by:
Hi, I have several <Div>'s that I have assigned an ID for. When the page loads It set the Absolute Position through a Style sheet. Can I then reference the <Div> through VB to move it to another...
5
by: Stan R. | last post by:
Greetings. I have a couple of questions concerning CSS layouts, as apposed to the old <tablemethod for creating layouts . Even after spending the last few days searching all over Google Groups, I...
5
by: Agix | last post by:
Hi there, Please check out : http://clarifysolutions.co.uk/certenroll/ The source is included below. This page is a test, so I can play about with paddings, margins and layouts using divs as...
6
by: plumba | last post by:
Hi I have a check box which when ticked displays the sumit buttons of a form and gives the checkbox field a value of "agree". I would like to set the check box to hide the section of the form...
1
by: milas57 | last post by:
hello everyone i have a homepage which introduce the company website as shown below im having trouble to find some javascript code when i click on the header link like servives the body content...
8
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.