How to use JQuery with Master Pages? How to use JQuery with Master Pages? asp.net asp.net

How to use JQuery with Master Pages?


EDIT

As @Adam points out in the comments, there is a native jQuery mechanism that basically does the same thing as the hack in my original answer. Using jQuery you can do

 $('[id$=myButton]').click(function(){ alert('button clicked'); }); 

My hack was originally developed as a Prototype work around for ASP.NET and I adapted it for the original answer. Note that jQuery basically does the same thing under the hood. I recommend using the jQuery way, though, over implementing my hack.

Original answer left for comment context

When you use a master page, ASP.NET mangles the names of the controls on the dependent pages. You'll need to figure out a way to find the right control to add the handler to (assuming you're adding the handler with javascript).

I use this function to do that:

function asp$( id, tagName ) {    var idRegexp = new RegExp( id + '$', 'i' );    var tags = new Array();    if (tagName) {        tags = document.getElementsByTagName( tagName );    }    else {        tags = document.getElementsByName( id );    }    var control = null;    for (var i = 0; i < tags.length; ++i) {       var ctl = tags[i];       if (idRegexp.test(ctl.id)) {          control = ctl;          break;       }    }    if (control) {        return $(control.id);    }    else {        return null;    }}

Then you can do something like:

jQuery(asp$('myButton','input')).click ( function() { alert('button clicked'); } );

where you have the following on your child page

<asp:Button ID="myButton" runat="server" Text="Click Me" />


If your site has content pages in other folders, using the Page's ResolveUrl method in the src path will ensure that your js file can always be found:

<script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery-1.2.6.min.js") %>' ></script>


Make sure that jQuery is being added in the master page. Given that you have this control:

<asp:Button ID="myButton" runat="server" Text="Submit" />

You can wireup the javascript with this:

$(document).ready(function() {    $('[id$=myButton]').click(function() { alert('button clicked'); });});

$(document).ready() fires when the DOM is fully loaded, and all the elements should be there. You can simplify this further with

$(function() {});

The selector syntax $('[id$=myButton]') searches elements based on their id attribute, but matches only the end of the string. Conversely, '[id^=myButton]' would match the beginning, but for the purposes of filtering out the UniqueID that wouldn't be very useful. There are many many more useful selectors you can use with jQuery. Learn them all, and a lot of your work will be done for you.

The problem is that ASP.Net creates a unique id and name attribute for each element, which makes finding them difficult. It used to be that you'd need to pass the UniqueID property to the javascript from the server, but jQuery makes that unneccessary.

With the power of jQuery's selectors, you can decouple the javascript from the server-side altogether, and wireup events directly in your javascript code. You shouldn't have to add javascript into the markup anymore, which helps readability and makes refactoring much easier.