how to attach javacript to BODY tag via codebehind

Discussion in 'ASP .Net' started by darrel, Mar 24, 2005.

  1. darrel

    darrel Guest

    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
    darrel, Mar 24, 2005
    #1
    1. Advertising

  2. darrel

    Karl Seguin Guest

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

    protected HtmlGenericControl body;

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

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    "darrel" <> wrote in message
    news:...
    >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
    >
    >
    Karl Seguin, Mar 25, 2005
    #2
    1. Advertising

  3. 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

    --

    Alphonse Giambrone
    Email: a-giam at customdatasolutions dot us


    "darrel" <> wrote in message
    news:...
    >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
    >
    >
    Alphonse Giambrone, Mar 25, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Chan
    Replies:
    1
    Views:
    573
    cbDevelopment
    Dec 3, 2005
  2. shruds
    Replies:
    1
    Views:
    709
    John C. Bollinger
    Jan 27, 2006
  3. Replies:
    0
    Views:
    631
  4. Anubhav star

    Need help in javacript

    Anubhav star, May 4, 2008, in forum: HTML
    Replies:
    2
    Views:
    334
    Jukka K. Korpela
    May 5, 2008
  5. Warren Tang
    Replies:
    1
    Views:
    528
    Warren Tang
    Sep 17, 2008
Loading...

Share This Page