Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q2 2011 released 07/12/2011
select

Ajax / Persisting JavaScript







  • RadAjax controls re-initialize JavaScript code after an AJAX request has been performed. This behavior is very important when working with third party controls inside the RadAjaxPanel that use rich JavaScript functionality.

    The example contains a JavaScript code block within the RadAjaxPanel that is evaluated on page load. The code holds some logic for browsing through a web galery of images. When an AJAX request is performed, the RadAjaxPanel re-initializes the JavaScript code and as a result the custom functionality works properly.

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page Language="vb" CodeFile="DefaultVB.aspx.vb" Inherits="Telerik.AJAXExamplesVBNET.AJAX.Examples.Common.PersistJavaScript.DefaultVB" %>

    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ Register TagPrefix="telerik" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <%@ Register TagPrefix="telerik" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="telerik" TagName="Header" Src="~/Common/Header.ascx" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/tr/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <telerik:HeadTag runat="server" ID="Headtag1"></telerik:HeadTag>
        <!-- custom head section -->
        <!-- end of custom head section -->
    </head>
    <body class="BODY">
        <form runat="server">
        <telerik:Header runat="server" ID="Header1" NavigationLanguage="VB"></telerik:Header>
        <!-- content start -->
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <div class="module" style="width: 350px; margin-right: 20px;">
            <telerik:RadAjaxPanel ID="RadAjaxPanel1" ClientEvents-OnResponseEnd="CreateScripts"
                runat="server" LoadingPanelID="LoadingPanel1">
    <asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Start ajax request"
    CssClass="qsfButtonBigger"></asp:Button>

                <br />
                <br />
                <br />
                <div id="Container" style="width: 350px; height: 244px;">
                </div>
                <br />
                <br />

                <script type="text/javascript">
                            /*<![CDATA[*/
                            function CreateScripts()
                            {
                                window.currentIndex = 0;
            
                                var MyImageCollection = new Array("img/img_1.jpg", "img/img_2.jpg", "img/img_3.jpg");
                                var MyImage = new Image();
                                MyImage.id = "myImg";
                                MyImage.src = "img/img_1.jpg";
                                MyImage.setAttribute('galleryimg', 'no');
        
                                var ControlsContainer = document.getElementById("Container");
        
                                function UpdateImage()
                                {
                                    NextButton.disabled = (window.currentIndex > MyImageCollection.length - 2)? true : false;
                                    PreviousButton.disabled = (window.currentIndex < 1)? true : false;
                                    MyImage.src = MyImageCollection[window.currentIndex];
                                }
        
                                var PreviousButton = document.createElement("button");
                                PreviousButton.innerHTML = "&laquo;&nbsp;previous";
                                PreviousButton.className = "qsfButton";
                                PreviousButton.style.marginBottom = 14 + "px";
                                PreviousButton.disabled = true;
                                PreviousButton.setAttribute('type','button');
                                PreviousButton.onclick = function(e)
                                {
                                    if(!e)
                                        var e = window.event;
                                    
                                    if(e.preventDefault)
                                    {
                                        e.preventDefault();
                                    }
                                    
                                    window.currentIndex--;
                                    UpdateImage();
                                };
        
                                var NextButton = document.createElement("button");
                                NextButton.innerHTML = "next&nbsp;&raquo;";
                                NextButton.className = "qsfButton";
                                NextButton.style.marginLeft = 14 + "px";
                                NextButton.style.marginBottom = 14 + "px";
                                NextButton.disabled = false;
                                NextButton.setAttribute('type','button');
                                NextButton.onclick = function(e)
                                {
                                    if(!e)
                                        var e = window.event;
        
                                    if(e.preventDefault)
                                    {
                                        e.preventDefault();
                                    }
        
                                    window.currentIndex++;
                                    UpdateImage();
                                };
        
                                ControlsContainer.appendChild(PreviousButton);
                                ControlsContainer.appendChild(NextButton);
                                ControlsContainer.appendChild(document.createElement("br"));
                                ControlsContainer.appendChild(MyImage);
                         };
                        
                         CreateScripts();
                            /*]]>*/
                </script>

            </telerik:RadAjaxPanel>
            <telerik:RadAjaxLoadingPanel ID="LoadingPanel1" runat="server" MinDisplayTime="300" />
        </div>
        <!-- content end -->
        <telerik:Footer runat="server" ID="Footer1"></telerik:Footer>
        </form>
    </body>
    </html>

Get more than expected!

 
 

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2024 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451