Thursday, December 3, 2009

Asp.net Master page and JQuery reference – JS Inside Master Pages

LavaLamp for JQuery lovers ! “ nice name you will find it when you try to google for JQuery Menu :) http://www.gmarwaha.com/blog/2007/08/23/lavalamp-for-jquery-lovers/ try It! .

I download it and create the example page on just 3 min :) and take 6 hour to apply it to my master page. all you need to do to add the JQuery plugins is to add these lines to you Master Head tag  (add Jquery references and lavalamp )

<head runat="server"  style=" height:100%;"  >
    <title></title>
 <script type="text/javascript"  src="../Js/jquery-1.2.3.min.js"></script>
   <script type="text/javascript"  src="../Js/jquery.easing.min.js"></script>
   <script type="text/javascript"  src="../Js/jquery.lavalamp.min.js"></script>
   <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            $(function() {
                      $("#1").lavaLamp({
                            fx: "backout",
                          speed: 700,
                           click: function(event, menuItem) {
                                 return false;
                            }
                          });        });        });
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
     </script>
</head>

Great you will get this error :

image 

This occurs of course since jQuery hasn't been loaded by the time the function tries to run.

Solution : 

1- Initially my script was placed below my head ContentPlaceHolder
2- If your MasterPage is not at the same directory level then things won't work either. You can use ResolveUrl
3-forces the header to resolve data binding expressions, and everything gets back to normal! by adding the below code to master page code behind

Header Code :

<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<script type="text/javascript"  src='<%# ResolveUrl ("~/Js/jquery-1.2.3.min.js") %>'></script>

Code Behind

 protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Page.Header.DataBind();
    }
hope my 6 hour of searching :) help someone avoid run into these problems.:) 

11 comments:

chris w said...

this indeed save me a lot of time. thanks

chris

Pablo A Castillo said...

thanks for sharing this! this save me a lot of time too.

Anonymous said...

Great ! thank you very much !
I fixed my error.
Leo

Supplement Pro said...

the master page not resolving the url was the problem. ugh...

Even though I already spent my own time on it, and google time... Your solution is ultimately the solution.

Only problem I had was
base.OnLoad(e);
I have to comment that line out as I get a stackoverflowexception error if not... Works without it, and long as i have the second line Page.Header.DataBind();

Many Thanks!

Anonymous said...

Many many thanks. Very nicely explained with screenshots and clear solution steps. You made my weekend.

5 Stars!

Mark said...

I was really happy to find this as it described my situation perfectly. However, even after doing everything you said, I am still getting the "object expected error". I am a real newbie. Can anybody help? Here is my code (right after the ContentPlaceHolder for "head"):



$(function() {
$('.example').truncate({ max_length: 24 });
});


"script" replace by "scropt" so I could post it.
Thanks, Mark

Mark said...

I notice that the script lines didn't post. I am using the example exactly as instructed from:
henrik.nyh.se/2008/02/jquery-html-truncate

Thanks for any help, Mark

Mordy said...

I want to bear your children! I've wrestled with this on and off on my last three projects - I've managed workarounds but this is the clean solution I've been looking for :)

Kunal said...

I done what u suggest. Still facing same problem.

Please help
Thanks
Kunal.pagariya@gmail.com

Anonymous said...

Thnks

Anonymous said...

Thank you so much!!! Worked like a charm!!!

Post a Comment

Followers

Blog Archive

Search

Loading...

About Me

My Photo
Waleed Mohamed
Flair Developer www.flair-systems.com
View my complete profile

Visitors Map

follow me