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

  • Thread starter Thread starter Guest
  • Start date Start date
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,
 
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
 
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 

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

Back
Top