All posts

ASP.NET Ajax Controls and jQuery

As you probably know by know the Q3 2008 release is out. In this release we included the jQuery JavaScript library in our scripts. For now only RadTreeView and RadScheduler benefit from it but we plan deeper jQuery integration in the future. In this blog post I will show you how to use the built-in jQuery file in your projects.

Including jQuery

If you have RadScheduler or RadTreeView in your page jQuery is already included so you can skip to the next paragraph. If not - read ahead.

  1. Add a ScriptReference pointing to Core.js as we use a slightly customized version of jQuery which depends on Core.js.

    Update!!! We have not modified the implementation of jQuery in any way. We have just appended a few more lines of JavaScript at the end of the file in order to avoid the aforementioned  version conflict and to include a few useful jQuery plugins.
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
  2. Add a ScriptReference pointing to jQuery.js
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />

Here is how your RadScriptManager should look like in the end:

<telerik:RadScriptManager runat="server" ID="RadScriptManager1" >
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
</Scripts>
</telerik:RadScriptManager>

Using jQuery

After including the jQuery file you can start using it. There is a trick though - the jQuery object is available as $telerik.$ instead of the default $ or jQuery aliases. This is so to avoid compatibility issues with applications which already use (other versions of) jQuery. For more info you can check the documentation of the noConflict method. Fortunately there is an easy workaround to enable back the $ alias:

<script type="text/javascript">
window.$ = $telerik.$;
</script>

or even better use a self-calling anonymous function to avoid creating a global variable (window.$):

<script type="text/javascript">
(function($) {
$(document).ready(function() {
alert("Hooray! The document is ready!");
}
);
})($telerik.$);
</script>

I much prefer the second option which in fact we use internally to avoid typing $telerik.$ every time.

Demo

Here is a modified version of the RadComboBox and jQuery integration demo which now uses the built-in jQuery library:

<telerik:RadScriptManager runat="server" ID="RadScriptManager1">
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
<asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
</Scripts>
</telerik:RadScriptManager>

<telerik:RadComboBox runat="server" ID="RadComboBox1" Width="300px" Skin="Telerik">
<Items>
<telerik:RadComboBoxItem Text="ASP.NET AJAX UI Controls" />
<telerik:RadComboBoxItem Text="WinForms UI Controls" />
<telerik:RadComboBoxItem Text="WPF UI Controls" />
<telerik:RadComboBoxItem Text="Silverlight UI Controls" />
<telerik:RadComboBoxItem Text="Telerik Reporting" />
<telerik:RadComboBoxItem Text="Telerik OpenAccess ORM" />
<telerik:RadComboBoxItem Text="Telerik Sitefinity CMS" />
</Items>
</telerik:RadComboBox>

<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('.rcbItem')
.mouseover(function() {
$(this).stop().animate({ paddingLeft: "30px" }, { duration: 300 });
})
.mouseout(function() {
$(this).stop().animate({ paddingLeft: "4px" }, { duration: 300 });
});
});
})($telerik.$);
</script>

[Download]

I hope this helps.

Facebook Twitter DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Add diigo bookmark

Comments  8

  • 19 Nov

    I get the JS error by following above example exactly.
    Do i need to include any additional JS files, besied doing this
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1">
    <Scripts>
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
    </Scripts>
    </telerik:RadScriptManager>


    ?????

    Stas

  • 19 Nov

    never mind, i figured out my issue, it works just fine!

    Stas

  • 01 Dec

    Hello Atanas,

    Please let me know which jquery plugins included in Telerik.Web.UI?

    Best regards

    Mostafa Anoosheh

  • 02 Dec

    For now we have included only the easing equations plugin.

    Atanas Korchev

  • 11 Feb

    If we are already deploying a newer version of jQuery, how can we alter the Telerik script so that it picks up that version instead?

    Nick Tulett

  • 12 Feb

    You cannot do that for the time being. However with the Q1 2009 release we will include jQuery 1.3.1 and also provide the ability to include your own version of jQuery which will override ours. Of course we cannot guarantee that our products will work seamlessly with other versions of jQuery.

    Atanas Korchev

  • 20 Feb

    How is it possible to integrate standard jQuery plug-ins within the $telerik implementation? I just keep getting "jQuery is undefined" errors.

    Nathan Pledger

  • 23 Feb

    I figured if I can't find out how to integrate Plug-Ins without modifying the plug-ins themselves, I was going to have to revisit why I couldn't get the "native" jQuery working without using $telerik.$.

    Turns out in your reference, make sure you do not self-close the script tag:

    <script type="text/javascript" src="js/jquery-1.3.1.js"></script>

    Nathan Pledger

Post a comment
  1. Formatting options
       
     
     
     
     
       
  2. Security image