Putting same Javascript function in every ASP.net page

Discussion in 'ASP .Net' started by Shikari Shambu, Aug 6, 2004.

  1. Hi,
    I have an ASP.NET website that has pages that inherit from a BasePage. I
    want all pages to have some common Javascript functions.

    I tried doing the following

    In the Base Page
    protected System.Web.UI.HtmlControls.HtmlGenericControl body;

    protected void Page_Load(object sender, System.EventArgs e)

    {


    body.Attributes.Add("onload","javascript:alert('Hello World')");

    }

    And, when I access the child page MyPage.aspx I get

    Object reference not set to an instance of an object.

    How do I add same javascript to the body onload tag of all my pages.

    TIA
     
    Shikari Shambu, Aug 6, 2004
    #1
    1. Advertising

  2. Shikari Shambu

    Juno Guest

    Hi,

    Did you put runat=server in body tag,like <body id=Body runat=server>
    or you may initiate a class before you use it,
    HtmlGenericControl body = new HtmlGenericControl("body");


    --
    Juno
    MCSD.NET, MCDBA, MCSE
    ----------------------------------------------------------
    Support Team of EasyDotNet, INC. http://www.EasyDotNet.com
    DataForm.NET - The most powerful data entry web server control for ASP.NET



    "Shikari Shambu" <> wrote in message
    news:...
    > Hi,
    > I have an ASP.NET website that has pages that inherit from a BasePage. I
    > want all pages to have some common Javascript functions.
    >
    > I tried doing the following
    >
    > In the Base Page
    > protected System.Web.UI.HtmlControls.HtmlGenericControl body;
    >
    > protected void Page_Load(object sender, System.EventArgs e)
    >
    > {
    >
    >
    > body.Attributes.Add("onload","javascript:alert('Hello World')");
    >
    > }
    >
    > And, when I access the child page MyPage.aspx I get
    >
    > Object reference not set to an instance of an object.
    >
    > How do I add same javascript to the body onload tag of all my pages.
    >
    > TIA
    >
    >
     
    Juno, Aug 6, 2004
    #2
    1. Advertising

  3. A lot of the times this happens, it's because you have the control
    referenced in a class, but don't have the control defined in the actual ASPX
    page. Make sure that on every page that inherits from this that the body tag
    has a runat="server" attribute and you have set the id to "body". Otherwise
    when it comes time to render it will have no idea where the tag is that it's
    supposed to add an attribute to.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage

    "Shikari Shambu" <> wrote in message
    news:...
    > Hi,
    > I have an ASP.NET website that has pages that inherit from a BasePage. I
    > want all pages to have some common Javascript functions.
    >
    > I tried doing the following
    >
    > In the Base Page
    > protected System.Web.UI.HtmlControls.HtmlGenericControl body;
    >
    > protected void Page_Load(object sender, System.EventArgs e)
    >
    > {
    >
    >
    > body.Attributes.Add("onload","javascript:alert('Hello World')");
    >
    > }
    >
    > And, when I access the child page MyPage.aspx I get
    >
    > Object reference not set to an instance of an object.
    >
    > How do I add same javascript to the body onload tag of all my pages.
    >
    > TIA
    >
    >
     
    Mark Fitzpatrick, Aug 6, 2004
    #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. Michelle Stone
    Replies:
    1
    Views:
    570
    Emilio
    Oct 15, 2003
  2. Jensen bredal
    Replies:
    2
    Views:
    6,190
    Eliyahu Goldin
    Oct 6, 2005
  3. Saurabh
    Replies:
    6
    Views:
    4,589
    Chris Smith
    May 30, 2004
  4. JackBlack
    Replies:
    3
    Views:
    1,402
    Jason Kester
    Sep 20, 2006
  5. =?Utf-8?B?SXJ3YW5zeWFo?=
    Replies:
    4
    Views:
    2,478
    =?Utf-8?B?SXJ3YW5zeWFo?=
    Oct 30, 2007
Loading...

Share This Page