Reference body tag from code-behind?

T

tshad

In VS 2003, I am trying to set the onload property of the body tag to "". I
do this all the time from my Dreamweaver pages (one page aspx page). But in
a code-behind page I need to set a reference. How do I reference the <body>
tag?

I have the <body> set as:

<body id="myBody" onload="document.forms[0].Textbox3.focus()">

I change this as:

myBody.Attributes.Add("onLoad","");

I get the error:

The type or namespace name 'myBody' could not be found (are you missing
a using directive or an assembly reference?)

I realize that I need to add a reference and tried to do:

protected System.Web.UI.HtmlControls.Body myBody;

I get the error:

The type or namespace name 'Body' does not exist in the class or
namespace 'System.Web.UI.HtmlControls' (are you missing an assembly
reference?)

What would the reference be?

Thanks,

Tom
 
T

tshad

Mark Rae said:
What would the reference be?

Ahem...

<body id="myBody" runat="server"
onload="document.forms[0].Textbox3.focus()">

You're right.

I missed the runat attribute. Fixed that.

But that doesn't help the reference problem. I still get the error.

Thanks,

Tom
 
P

Patrice

Also try an HtmlGenericControl. There is no html control specific to the
body tag...

--
Patrice

tshad said:
Mark Rae said:
What would the reference be?

Ahem...

<body id="myBody" runat="server"
onload="document.forms[0].Textbox3.focus()">

You're right.

I missed the runat attribute. Fixed that.

But that doesn't help the reference problem. I still get the error.

Thanks,

Tom
 
P

Phil H

tshad said:
Mark Rae said:
What would the reference be?

Ahem...

<body id="myBody" runat="server"
onload="document.forms[0].Textbox3.focus()">

You're right.

I missed the runat attribute. Fixed that.

But that doesn't help the reference problem. I still get the error.

Thanks,

Tom

Dear Tom

I don't think it's possible because, as the error message points out,
there isn't a class in System.Web.UI.HtmlControls representing it.

In other words Microsoft have not provided any mechanism to access the
<body> .. </body> element of the web page programmatically.

To be honest I don't see why this should be necessary. You probably
need to think about the approach you are taking in your solution. There
must be an easier and more direct way of carrying out the task using
normal web server controls.

Phil H
 
J

Jon Paal

take this out of the body tag:

onload="document.forms[0].Textbox3.focus()"

assign onload event programmatically as needed


tshad said:
Mark Rae said:
tshad said:
What would the reference be?

Ahem...

<body id="myBody" runat="server" onload="document.forms[0].Textbox3.focus()">

You're right.

I missed the runat attribute. Fixed that.

But that doesn't help the reference problem. I still get the error.

Thanks,

Tom
 
M

Mark Rae

In other words Microsoft have not provided any mechanism to access the
<body> .. </body> element of the web page programmatically.

Apart from the HtmlGenericControl object, of course... :)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

The declaration for the reference should be:

protected System.Web.UI.HtmlControls.HtmlGenericControl myBody;

If you declare it with any other class, it will not be recognised as the
same object, and you will just get a reference that is not attached to
anything.
Mark Rae said:
What would the reference be?
Ahem...

<body id="myBody" runat="server"
onload="document.forms[0].Textbox3.focus()">

You're right.

I missed the runat attribute. Fixed that.

But that doesn't help the reference problem. I still get the error.

Thanks,

Tom
 
T

tshad

Phil H said:
tshad said:
Mark Rae said:
What would the reference be?

Ahem...

<body id="myBody" runat="server"
onload="document.forms[0].Textbox3.focus()">

You're right.

I missed the runat attribute. Fixed that.

But that doesn't help the reference problem. I still get the error.

Thanks,

Tom

Dear Tom

I don't think it's possible because, as the error message points out,
there isn't a class in System.Web.UI.HtmlControls representing it.

In other words Microsoft have not provided any mechanism to access the
<body> .. </body> element of the web page programmatically.

To be honest I don't see why this should be necessary. You probably
need to think about the approach you are taking in your solution. There
must be an easier and more direct way of carrying out the task using
normal web server controls.

It is necessy as I am using the body tag to put focus on a textbox when the
page loads. But I need to change the onLoad event to either disable it or
jump to another control as I am hiding the control on the screen after the
initial Page_load.

As I mentioned, this is no problem in a single page design. But if there is
no way to do this using code-behind then this is just another reason why I
am glad I normally build my pages using DW instead of VS.

Thanks,

Tom
 

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,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top