Telerik Telerik
The Telerik Blogs

ASP.NET Ajax Controls and jQuery

Thursday, November 06, 2008 by Atanas Korchev | Comments 15

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.

15 Comments

  • Stas 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!
  • Mostafa Anoosheh 01 Dec

    Hello Atanas,

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

    Best regards

  • Atanas Korchev 02 Dec
    For now we have included only the easing equations plugin.
  • Nick Tulett 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?
  • Atanas Korchev 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.
  • How is it possible to integrate standard jQuery plug-ins within the $telerik implementation? I just keep getting "jQuery is undefined" errors.
  • 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>

  • Carl Wagear 14 May

    I seem to be able to trigger custom jquery functions if I associate with events/classes of the Rad controls themselves, as per this blog's example.
     

    I’ve been trying to get a standard button or other asp.net control to call a jquery function.

     

    We want to use the slideUp/slideDown animation on a <div> or <panel> elsewhere in  the page and this does work if I use Rad/telerik controls such as the RadComboBox  example to trigger the function.

     

    But I couldn’t get a ‘regular’ ASP.NET control such as a button to work.

     

    Has anyone used the embedded jQuery to manipulate non-telerik elements?

  • Carl Wagar 14 May

    I’m trying this. Can’t get it to react to the button.

     

    <script type="text/javascript">

        (function($) {

        $('#mybut').click(function() {

            $(document.getElementById('book')).animate({

                opacity: 0.80,

                height: 'toggle'

            }, 500, function() {

                // Animation complete.

            })

        }); 

        })($telerik.$);

     </script>

     

    <div>

      <input type="button" name="mybut" id="mybut" value="Click Me" />

    </div>

     

    <div id="book">

    Text to rollup<br>

    Text to rollup<br>

    Text to rollup<br>

    Text to rollup<br>

    Text to rollup<br>

    Text to rollup<br>

    Text to rollup<br>

    Text to rollup<br>

     

    </div>

  • Carl Wagar 14 May
    OK, I figured it out. I had to declare the button above the scripting and I had to use getElementById

     

    <div id="clickme">

     

     

    <input type="button" name="mybut" id="mybut" value="Click Me" />

     

    </

     

    div>

     

     

    <script type="text/javascript">

     

    (

    function($) {

     

    $(document.getElementById(

    'mybut')).click(function() {

     

    $(document.getElementById(

    'book')).animate({

     

    opacity: 0.80,

    height:

    'toggle'

     

    }, 500,

    function() {

     

     

    // Animation complete.

     

    })

    });

    })($telerik.$);

     

    </script>

     

  • Very .. usefull article. Thank you very much.
  • C 29 Jul
    I've been using Telerik controls for a while now. They look pretty, but they have done nothing but given me trouble. Now I have to spend all this time removing all the controls from my projects so I can use jQuery without a huge hassle.

    With responses like:

    "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."

    As a staple from the telerik team, I see no incentive to use their controls any further. Ridiculous.
  • bstensrud 13 Mar
    I have the code above in a working solution, but, after a selection is made ( of one of the buttons ) the events are never attached again.  Any Idea please ?
  • Eduardo Bonato 06 Apr
    There is any way to use builtin jquery ref on header? RadScriptManager needs to be inside a forms element whichs resides in body. This way we can't handle DOM loading before UI gets to the user..

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)