[CLOSED] "Ext is undefined" - error in simple javascript when calling DirectMethod

  1. #1

    [CLOSED] "Ext is undefined" - error in simple javascript when calling DirectMethod

    So I have my Default.aspx and trying to do some "keepalive" so that the session doesnt time out all the time.

    The page inherits a MasterPage and in the head I place this:
    <%@ Page Language="C#" MasterPageFile="SiteSecure.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationExtNetTest.Secure.Default" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <asp:Content ID="asd" ContentPlaceHolderID="head" runat="server">
        <ext:XScript runat="server">
            <script type="text/javascript">
                var timerObj;
    
                function KeepAlive() {
                    #{DirectMethods}.SendKeepAlive();
                    timerObj = setTimeout("KeepAlive()", 1000 * 30);
                }
    
                KeepAlive();
            </script>
        </ext:XScript>
    </asp:Content>
    The code-behind:

    [DirectMethod]
    public void SendKeepAlive()
    {
        DateTime start = DateTime.Now;
        KeepAlivePingWithReply asd = new KeepAlivePingWithReply();
        asd = (KeepAlivePingWithReply)SRef.main.SendRequest(asd);
    
        if (asd != null && asd.Success)
        {
            TimeSpan ts = DateTime.Now.Subtract(start);
            ToolbarTextItem2.Text = asd._ServerTime.ToLongTimeString() + " (" + ts.TotalMilliseconds + " ms)";
        }
        else
        {
            ToolbarTextItem2.Text = "No server reply";
            X.Msg.Alert("Error", "EPIC FAIL");
        }
    }
    Easy enough. Problem is that I get the following error in the browser:

    Uncaught ReferenceError: Ext is not defined

    I have also tried this approach in calling DirectMethod:
               function KeepAlive() {
                    Ext.net.DirectMethods.SendKeepAlive();
                    timerObj = setTimeout("KeepAlive()", 1000 * 30);
                }
    But same error. Its weird, because I call DirectMethods from Javascript in other pages without problems. I cant see the error...
  2. #2
    Hi,

    Do you have ResourceManager on the master page?
  3. #3
    yes, the entire MasterPage is here:



    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteSecure.master.cs" Inherits="WebApplicationExtNetTest.Secure.SiteSecure" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>test</title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
        <div>
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
        </div>
        </form>
    
       
    </body>
    </html>
  4. #4
    Hi,

    Try this in master page head (i think that your script is executed before ExtJS resources loading)
    <head runat="server">
        <title>test</title>
        <ext:ResourcePlaceHolder runat="server" />
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
  5. #5
    Done, but that didnt help.

    Can you reproduce the error in this test page?


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationExtNetTest.Test.Default" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <ext:XScript ID="XScript1" runat="server">
            <script type="text/javascript">
                var timerObj;
    
                function KeepAlive() {
                    Ext.net.DirectMethods.SendKeepAlive();
                    timerObj = setTimeout("KeepAlive()", 1000 * 30);
                }
    
                KeepAlive();
            </script>
        </ext:XScript>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <div>
        
        </div>
        </form>
    </body>
    </html>

    Code-behind:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Ext.Net;
    
    namespace WebApplicationExtNetTest.Test
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [DirectMethod]
            public void SendKeepAlive()
            {
            }
        }
    }
  6. #6
    I have reproduced.

    Well, Ext scripts are not loaded at the moment of KeepAlive() executing.

    Please use Ext.onReady() or DocumentReady listener of ResourceManager.
  7. #7
    Hi,

    1. You have to use ResourcePlaceHolder and place your script block after that control (it is better to place it to the head) to ensure that Ext.Net resources will be loaded first

    2. You have to wrap your script block by "Ext.onReady" because Ext.Net code (widgets and init of direct method) are executed after DOM tree is built

    <%@ Page Language="C#" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        [DirectMethod]
        public void SendKeepAlive()
        {
        }
    </script>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        
        <ext:ResourcePlaceHolder runat="server" />
        
        <ext:XScript ID="XScript1" runat="server">
            <script type="text/javascript">            
                Ext.onReady(function(){
                    var timerObj;
     
                    function KeepAlive() {
                        Ext.net.DirectMethods.SendKeepAlive();
                        timerObj = setTimeout("KeepAlive()", 1000 * 30);
                    }
         
                    KeepAlive();
                }, this, {delay:100});
                
            </script>
        </ext:XScript>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <div>
         
        </div>
        </form>
    </body>
    </html>
  8. #8
    Thanks, thats one step further. Now the "ext is undefined" is gone and the DirectMethod is executed once.

    What seemed to "do it" was the <ext:ResourcePlaceHolder runat="server" /> that I did not have before.

    However, the next run (after 30 seconds) fails since it cannot find the method KeepAlive... Any bright ideas here?
  9. #9
    Hi,

    It is scope issue, you pass string to setTimeout, that string assumes to call js method which is global (method belong to document.window object)
    But now, after wrapping by Ext.onReady, that method is not global anymore therefore it should be called as in the following code (pass KeepAlive method directly to setTimeout)
    function KeepAlive() {
          Ext.net.DirectMethods.SendKeepAlive();
          timerObj = setTimeout(KeepAlive, 1000 * 3);
    }
    What seemed to "do it" was the <ext:ResourcePlaceHolder runat="server" /> that I did not have before.
    That control defines the place for rendering Ext.Net resources (scripts and styles), there is Mode property where you can set required resource type
  10. #10
    Thanks both of you for quick and great support and help!

    I actually managed to do it this way (even though Vladimirs suggestions seems cleaner):

        <ext:XScript ID="XScript1" runat="server">
            <script type="text/javascript">
                var test;
                Ext.onReady(function () {
                    var timerObj;
    
                    test = function () {
                        Ext.net.DirectMethods.SendKeepAlive();
                        timerObj = setTimeout(test, 1000 * 30);
                    }
    
                    test();
                }, this, { delay: 100 });
    
                 
            </script>
        </ext:XScript>

Similar Threads

  1. [CLOSED] "True is not defined" javascript error
    By coleg123 in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Aug 11, 2011, 6:51 PM
  2. Replies: 8
    Last Post: May 30, 2011, 5:55 PM
  3. Replies: 12
    Last Post: Apr 13, 2011, 3:28 PM
  4. Replies: 11
    Last Post: Mar 30, 2011, 2:21 PM
  5. Replies: 0
    Last Post: Mar 29, 2011, 5:32 PM

Tags for this Thread

Posting Permissions