Output literal to documents HEAD and BODY tags.

B

Big D

Is there any way to, from the page class, send literal output to the <HEAD>
of te page, or within the <BODY onload="('theFunctionIAddedToTheHead');"> ?
I need to do this in a class that inherrits from PAGE, but is an abstract
class for the pages of my site. I.E.

System.Web.UI.Page -> myBasePage

All pages in the site inherit from myBasePage, and will be able to use
funciton is myBasePage to modify content in the HEAD tag.

I've looked into inherriting from a class that totally overrides the base
pages' render method, but this is, quite honestly, a little scary to me
because it require re-creating the form entirely.

Ideas are greatly appreciated!


tnx

D
 
K

Kevin Spencer

Typically, to render HTML to the <HEAD> or <BODY> tags, or any other tag
that sits outside of a WebForm, you would do 2 things:

1. Add runat=server and an id to the tag in the Page Template
2. Add the CodeBehind class (HtmlGenericControl) to the CodeBehind.

The Page Template is incomplete without the CodeBehind class from which it
is derived. The problem in your case is that if you want these to be in an
inherited class, you're still going to have to add them in manually to each
Page Template that uses the CodeBehind. Although this would be a bit dodgy,
I believe it would work. Since it has to be added to each Page Template in
any case, an alternative would be to simplify your inherited Page class to
leave this out, and add it only to the Pages that need it.

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

Jeffrey Tan[MSFT]

Hi,

Thanks for posting in this group.
Just Kevin said, you can make your body tag into a HtmlGenericControl, then
you can manipulate it in code behind.
Like this:

<body MS_POSITIONING="GridLayout0" runat="server" id="bodyid"> //In html
view

//In Code behind
protected System.Web.UI.HtmlControls.HtmlGenericControl bodyid;

private void Page_Load(object sender, System.EventArgs e)
{
bodyid.Attributes.Add("bgcolor","red");
}

Then your body tag will be added bgcolor attribute after render. You also
can add event handler through this way.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top