Adding the ClientClose function on the fly

2 posts, 0 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 30 Nov 2011 Link to this post

    OK. I know I can do this using the .add_close() method on the client-side event.

    However, I need to add the function in such a way as to allow me to access objects from the calling functio9n in the callback function. So, I have something like this...
    function ProcessToolTipAction(action, ID)
    {
        if (action == "<%= GlobalConstants.CommandName.REQUESTINVITE %>")
        {
            var wnd = $find('<%=winInviteSelf.ClientID %>');
            wnd.add_close(function (sender, e)
            {
                var args = e.get_argument();
                if (IsDialogResultOK(args))
                {
                    AjaxRequestObject = GetAjaxRequestObject(action, "<%=this.GetType().ToString()%>");
                    AjaxRequestObject.AppointmentId = ID;
                    AjaxRequestObject.HideInvite = args.HideInvite;
                    AjaxRequestObject.SpecialNotes = args.SpecialNotes;
                    AjaxRequestJson(AjaxRequestObject);
                }
            });
            wnd.show();
        }
    }
    And this seems to work just fine.

    Except.

    The server-size code, invoked by my AjaxRequestJson() call is executed multiple times if the window is opened multiple times.

    Now, if I were doing this in the more traditional way (with the callback function being a standalone function) I'd just call remove_close(functionname) in the callback function itself.

    So, to the question: what do I pass to the remove_close() function to prevent multiple copies of the nested function from being called?

    --
    Stuart
  2. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 30 Nov 2011 Link to this post

    Ha!

    Sorted.
    function ProcessToolTipAction(action, ID) 
        if (action == "<%= GlobalConstants.CommandName.REQUESTINVITE %>"
        
            var wnd = $find('<%=winInviteSelf.ClientID %>'); 
            var func = function (sender, e) 
            
                wnd.remove_close(func);
                var args = e.get_argument(); 
                if (IsDialogResultOK(args)) 
                
                    AjaxRequestObject = GetAjaxRequestObject(action, "<%=this.GetType().ToString()%>"); 
                    AjaxRequestObject.AppointmentId = ID; 
                    AjaxRequestObject.HideInvite = args.HideInvite; 
                    AjaxRequestObject.SpecialNotes = args.SpecialNotes; 
                    AjaxRequestJson(AjaxRequestObject); 
                
            }
            wnd.add_close(func); 
            wnd.show(); 
        
    }

    Neat! Gotta love JavaScript!

    --
    Stuart
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top