Using javascript on pages that use a master page

Last post 11-19-2007 3:09 PM by Kevin.McPhail. 2 replies.

Sort Posts:

  • Using javascript on pages that use a master page

    11-19-2007, 10:37 AM

    I was wondering if i could get any guidance on using javascript for pages that make use of a masterpage. To give a specific example i would like to implement the delay load described by Matt Berseth here: http://mattberseth.com/blog/2007/07/delay_load_an_updatepanel.html. In the example he implements a lot of javascript that is specific to the page so i don't think it makes sense to add it to a script library. But how do i use it in a page inherited from a master page. The js is specific to the content of the page so i would not want to put it in the master page and you can't add a <script> </script> to content pages. It als seems tedious to use a stringbuilder and registerclientscriptblock from the code-behind (seems like i remember reading that this could be a problem with Ajax enabled pages anyway). So how are people managing ajax w/ custom js in content pages? Any pointers greatly appreciated.

     

    Thanks,

    Kevin

  • Re: Using javascript on pages that use a master page

    11-19-2007, 11:01 AM
    Answer

    Kevin.McPhail:
     and you can't add a <script> </script> to content pages.

    You most certainly can!  Typically I place it right after the <asp:Content> tag at the top of the page.  Just a few things to remember, if you are going to reference server controls in your javascript, use the client ID to reference the control such as:

        document.getElementById('<%= MyControl.ClientID %>').value

    rather than

        document.getElementByID('MyControl').value.

    Because IDs get mangled/changed a bit by the master/content system.  Also, if you have a scriptmanager or scriptmanagerproxy on your content page, place your scripts after that.

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: Using javascript on pages that use a master page

    11-19-2007, 3:09 PM

    Holy smokes! I feel like an idiot. Can't believe i did not try that before posting.

Page 1 of 1 (3 items)