Newbie: Is it possible to set <body> attributes via code behind?

G

Guest

I'm sure it's possible, but it hasn't occurred to me "how" to set the values
of the <body> tag via code-behind. I am currently setting the onload event in
the body tag of an ASP.NET web form using old-fashion asp type <%.. code...
%> blocks.

This "spaghetti-scripting" is difficult to manage, and is essentially a
hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
further causes problems when i try to wire events.

I tried, for example, to embbed an <asp:label /> and other controls in the
<body> tag, but VS.NET doesn't let me (probably for good reason too).

Anyone have ideas?

Thanks,
 
V

vMike

charliewest said:
I'm sure it's possible, but it hasn't occurred to me "how" to set the values
of the <body> tag via code-behind. I am currently setting the onload event in
the body tag of an ASP.NET web form using old-fashion asp type <%.. code...
%> blocks.

This "spaghetti-scripting" is difficult to manage, and is essentially a
hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
further causes problems when i try to wire events.

I tried, for example, to embbed an <asp:label /> and other controls in the
<body> tag, but VS.NET doesn't let me (probably for good reason too).

Anyone have ideas?

Thanks,


Give the body an id and a runat=server. For example <body id=body1
runat=server>. In codebehind (vb) put
Protected body1 as htmlgenericcontrol in your declarations. Then you can set
any attribute that you can set in html. Here is an example of setting the
onload attribute.

body1.attributes("onload") ="javascript:doSomething();return false;"

Mike
 
B

Brock Allen

Mark <body runat=server id=_body> then manually in your codebehind add:

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

And then you should be abe to use _body.Attributes.Add() and any other API
to modify the element.
 
C

Curt_C [MVP]

throw a RUNAT=SERVER in it and declare it in the codebehind. It should
work....
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top