Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomEvent.addHandler method.

11 posts, 0 answers
  1. none123456
    none123456 avatar
    17 posts
    Member since:
    Aug 2007

    Posted 04 Mar 2008 Link to this post

    I believe I've found a bug with the JavaScript for RadGrid.

    In dispose, it has:

    if(this._onResizeDelegate){

    $removeHandler(window,"resize",this._onResizeDelegate);

    this._onResizeDelegate=null;

    }

    This part of the code seems fine.
    However, _initializeDimensions has:

    if(navigator.userAgent.toLowerCase().indexOf("msie")!=-1){

    this._onResizeDelegate=Function.createDelegate(this,this.onWindowResize);

    setTimeout(function(){

    $addHandler(window,"resize",_1._onResizeDelegate);

    },0);

    }else{

    this._onResizeDelegate=Function.createDelegate(this,this.onWindowResize);

    $addHandler(window,"resize",this._onResizeDelegate);

    }

    The problem is that the resize delegate is saved before the handler is actually added via a call to setTimeout. If the window's location changes before this setTimeout method runs, an exception is thrown because $removeHandler is called when $addHandler never was.
    I believe the corrected code would be as follows:

    if(navigator.userAgent.toLowerCase().indexOf("msie")!=-1){

    setTimeout(function(){

    this._onResizeDelegate=Function.createDelegate(this,this.onWindowResize);

    $addHandler(window,"resize",_1._onResizeDelegate);

    },0);

    }else{

    this._onResizeDelegate=Function.createDelegate(this,this.onWindowResize);

    $addHandler(window,"resize",this._onResizeDelegate);

    }

    Simply move setting the resize delegate inside anonymous delegate executed by setTimeout. This way, if setTimeout never runs, the resize delegate is null and $removeHandler is never called.

    If necessary, I can post an example of this error. It's easily reproduced via something like a client-side refresh/redirect.

    Thanks for looking into this.

    David

  2. Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 07 Mar 2008 Link to this post

    Hello dmatson,

    Thank you for contacting us.
    We are not aware of such a problem - it seems there is something specific in your setup. Could you open a support ticket and send a sample runnable project that demonstrates the issue? Once we are able to reproduce the problem we will do our best to find the source of the problem.

    Regards,
    Plamen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. none123456
    none123456 avatar
    17 posts
    Member since:
    Aug 2007

    Posted 10 Mar 2008 Link to this post

    I'm able to reproduce the problem on a simple page as long as client scrolling is turned on. The example below doesn't reproduce the problem on every single load, but it was reproducing it about 80% of the time for me. (Whether the problem occurs depends on the timing of the setTimeout call in RadGrid - whether it runs before or after my reload method.)

    Default.aspx:

    <%

    @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <%

    @ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

    <!

    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 Page</title>

    </

    head>

    <

    body>

    <form id="form1" runat="server">

    <telerik:RadGrid ID="testGrid" runat="server">

    <ClientSettings>

    <Scrolling AllowScroll="true" />

    </ClientSettings>

    </telerik:RadGrid>

    <asp:ScriptManager ID="scriptManager" runat="server" EnablePartialRendering="true" />

    <script type="text/javascript">

    function onLoad() {

    Sys.Application.initialize();

    reload();

    }

    function reload() {

    if (window.location.href.indexOf('?redirected') == -1) {

    window.location.replace(window.location.href +

    '?redirected=true');

    }

    }

    window.onload = onLoad;

    </script>

    </form>

    </

    body>

    </

    html>


    Default.aspx.cs:

    using

    System;

    public

    partial class _Default : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    TestDataItem[] dataSource = new TestDataItem[] { new TestDataItem("Test 1A", "Test 1B"), new TestDataItem(

    "Test 2A", "Test 2B") };

    testGrid.DataSource = dataSource;

    testGrid.DataBind();

    }

    private class TestDataItem

    {

    private string _item1;

    public string Item1

    {

    get { return _item1; }

    set { _item1 = value; }

    }

    private string _item2;

    public string Item2

    {

    get { return _item2; }

    set { _item2 = value; }

    }

    public TestDataItem(string item1, string item2)

    {

    _item1 = item1;

    _item2 = item2;

    }

    }

    }


    David
  5. John
    John avatar
    416 posts
    Member since:
    Oct 2006

    Posted 13 Mar 2008 Link to this post

    dmatson,

    The following code example demonstrates how to achieve your goal:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
     
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
     
    <!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"
        <title>Untitled Page</title> 
    </head> 
    <body> 
        <form id="form1" runat="server"
            <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
            <div> 
                <telerik:RadGrid ID="testGrid" runat="server"
                    <ClientSettings> 
                        <Scrolling AllowScroll="true" /> 
                    </ClientSettings> 
                </telerik:RadGrid> 
                 
                <script type="text/javascript"
                function pageLoad()  
                { 
                    setTimeout("reload()", 0); 
                } 
     
                function reload()  
                { 
                    if (window.location.href.indexOf('?redirected') == -1)  
                    { 
                        window.location.replace(window.location.href + '?redirected=true'); 
                    } 
                } 
                </script>             
            </div> 
        </form> 
    </body> 
    </html> 
     


    load event - Raised after all scripts have been loaded and all objects in the application that are created by using $create are initialized. The load event is raised for all postbacks to the server, which includes asynchronous postbacks.

    If you are a page developer, you can create a function that has the name pageLoad, which automatically provides a handler for the load event. The pageLoad handler is called after any handlers that have been added to the load event by the add_load method.

    The load event takes an eventargs parameter, which is an Sys.ApplicationLoadEventArgs object. You can use the event arguments to determine whether the page is being refreshed as a result of a partial-page update and what components were created since the previous load event was raised.


    Regards...
    <John:Peel />
  6. none123456
    none123456 avatar
    17 posts
    Member since:
    Aug 2007

    Posted 13 Mar 2008 Link to this post

    John,

    Thanks for the assistance, but unfortunately that's not what I'm trying to do. The problem is a bug in the Grid that only appears if the user leaves the page between grid initialization and the setTimeout call to register the handler. When that happens, dispose tries to remove a handler that was never added. My sample is an attempt to reproduce this bug programatically.

    David
  7. terrysmith
    terrysmith avatar
    127 posts
    Member since:
    Nov 2006

    Posted 24 Mar 2008 Link to this post

    We are experiencing the same issue with the RadSplitter control. Because of an issue with VS2008 we had to roll back from the .NET 3.5 framework to .NET 3.0. After that, we started getting the same exception, "..."Handler was not added through the Sys.UI.DomEvent.addHandler method". I wonder if the two are related?



  8. John
    John avatar
    416 posts
    Member since:
    Oct 2006

    Posted 25 Mar 2008 Link to this post

    dmatson,


    I have found a forum link from the asp net forum which discusses a similar issue:

    http://forums.asp.net/p/1055781/1705462.aspx



    Hope this helps...
    <John:Peel />
  9. StephenWRogers
    StephenWRogers avatar
    31 posts
    Member since:
    Dec 2006

    Posted 29 Jul 2008 Link to this post

    I'm having the same problem as the original poster - an intermittent error saying "Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomEvent.addHandler method."

    It is always specifically to do with removing
    _onResizeDelegate from the window.resize event.

    The original poster pinpointed the error in the code - see below:

    if(navigator.userAgent.toLowerCase().indexOf("msie")!=-1){ 
     
    this._onResizeDelegate=Function.createDelegate(this,this.onWindowResize); 
     
    setTimeout(function(){ 
     
    $addHandler(window,"resize",_1._onResizeDelegate); 
     
    },0); 
     
     
     
     
     

    _onResizeDelegate is created, but the attachment is done inside a setTimeout call, presumably to make sure all the DOM content is loaded before running.  But dispose runs:

    if(this._onResizeDelegate){ 
     
    $removeHandler(window,"resize",this._onResizeDelegate); 
     
    this._onResizeDelegate=null
     

    So there will always be a possibility that _onResizeDelegate will exist here, but not be attached.(if another piece of script redirects the page say before the handler can be attached).

    This would cause the observed error.

    The original poster even suggested a fix - move the creation of the delegate inside the setTimeout function.

    Can this be fixed?  It's causing quite serious errors in our site.

    Thanks,

    Stephen





  10. Plamen Peshev
    Admin
    Plamen Peshev avatar
    886 posts

    Posted 29 Jul 2008 Link to this post

    Hi StephenWRogers,

    Could you open a support ticket and send a sample runnable project that demonstrates the issue? Once we are able to reproduce the problem we will do our best to find the source of the problem.

    Greetings,
    Plamen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  11. StephenWRogers
    StephenWRogers avatar
    31 posts
    Member since:
    Dec 2006

    Posted 29 Jul 2008 Link to this post

    Sure, I'll try to create something - although the original poster had included code that reproduced the error too.

    "The source of the problem" seems quite apparent though, there's a fairly obvious race condition here, that means if dispose runs before the setTimeout($addHandler) code, you'll get the error.

    Will try to upload a project.

    Thanks,

    Stephen
  12. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 30 Jul 2008 Link to this post

    Hello StephenWRogers,

    We will be expecting your example once you are ready with it.

    Best regards,
    Stephen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017