How to get rad control object in javascript from custom Sharepoint control

4 posts, 0 answers
  1. Herb Ramos
    Herb Ramos avatar
    10 posts
    Member since:
    Feb 2006

    Posted 04 Nov 2007 Link to this post

    Hi,

    I'm creating a custom sharepoint control (ascx) contains has a RadTreeView control.  I am trying to get the tree control instance from a javascript
    function as follows:

       // Gets a handle to the TreeView.
       function GetTreeControl()
       {
           // Get a handle to the TreeView.
           var tree =  document.getElementById('radTreeModule');
        //var tree =  document.getElementById('<%= radTreeModule.ClientID %>');
           if ( tree == null || tree == undefined )
               return null;
           return tree;
       }  

    Note: i tried two options for getElemenetById.

    I get null when using 
           var tree =  document.getElementById('radTreeModule');

    The tree nodes don't behave properly when using
       var tree =  document.getElementById('<%= radTreeModule.ClientID %>');



    I read somewhere that the INamingContainer implemented by Sharepoint may not support document.getElementById('<%= xxx.ClientID %>'); syntax.

    So how do I get the tree object for this custom control in javascript within the custom control?






  2. Nikolay
    Admin
    Nikolay avatar
    3997 posts

    Posted 06 Nov 2007 Link to this post

    Hi Herb Ramos,

    Thanks for writing to us and your question.

    Actually, the proper way to obtain the treeview client-side instance is using the following syntax:

    var tree = <%= radTreeModule.ClientID %>;
    or
    var tree = window["<%=
    radTreeModule.ClientID  %>"];

    Thus, you will get the client side object and a reference to all its properties and methods exposed at the client-side.

    Hope this helps.

    Regards,
    Nick
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center

  3. Sean
    Sean avatar
    10 posts
    Member since:
    Oct 2007

    Posted 25 Feb 2008 Link to this post

    I've implemented this solution to great success, however the decision was made recently to move the entire in-page set of javascript functions to a .js file.  Of course, this has the unfortunate side-effect of the page being unable to process the <%= %> tag from within the .js file, and/or losing context for the control.

    I would like to know if there's some way of referencing these controls through JavaScript if running from an included external file.

    There are a few other solutions I can think of, such as leaving the functions for any affected controls on the page (somewhat defeating the purpose), or passing in the "window[]" code as parameters to those functions when the appropriate controls that fire them have their attributes set -- which, even assuming it works, unnecessarily duplicates code, makes things harder to debug, and just seems like an all-around anti-pattern. 

    (As for why precisely I have to make this work from a .js file, let's just take it as read that I do.)

    ~Sean
  4. Nikolay
    Admin
    Nikolay avatar
    3997 posts

    Posted 26 Feb 2008 Link to this post

    Hello Sean,

    I believe that the only way would be using window["RadTreeView1"], where RadTreeView1 is the exact client id of the tree.

    Sincerely yours,
    Nick
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top