how to attach javacript to BODY tag via codebehind

D

darrel

I have a page that I need to attach a javascript to the BODY tag when the
page first loads (but not on postback).

Is there a way to do that via codebehind?

I've attached javascript to elements before like this:

Form1.Attributes.Add("onSubmit", "myOnSubmitEventHandler();")

But the problem is that there doesn't seem to be a
System.Web.UI.HtmlControls declaration for the BODY tag.

I can actually get around the problem with a few hacks, but it'll add some
muddy HTML to the page and I'd prefer to avoid that.

-Darrel
 
K

Karl Seguin

Use the HtmlGenericControl...
<body id="body" runat="server">

protected HtmlGenericControl body;

body.Attributes.Add(..., ...);
 
A

Alphonse Giambrone

If you mean that you want the script to run when the page first loads, but
not on postback, look at RegisterStartupScript.

In your page load event:
If Not IsPostback then
Dim strScript as String = "<script language='javascript'> " _
& "Your script goes here</script>"
Me.RegisterStartupScript("NameForScript", strScript)
End If
 

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

Top