Unable to find treeview control using $find()

7 posts, 1 answers
  1. Ken Jones
    Ken Jones avatar
    41 posts
    Member since:
    Sep 2009

    Posted 03 Sep 2009 Link to this post

    Hello,

    I have added a new button to a radeditor control which is working fine, however when I try and access controls within the aspx page that is displayed using the $find() method I am getting null values returned.

    This is the javascript that I am using:

    var treeview = $find("<%=uiRadTreeView.ClientID %>");

    However, when I try and access any properties of treeview I get javascript errors, for example:

    var node = treeview.findNodeByValue(nodeId);

    Gives me the following error:

    Microsoft JScript runtime error: 'null' is null or not an object.

    Any help would be much appreciated.

    Thanks,

    Ken

     

  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 03 Sep 2009 Link to this post

    Hello Ken Jones,

    The $find routine can fail in two cases:
    1. A control with the specified ID does not exist at all
    2. The control with the specified ID has not been initialized yet. This may occur if you are calling $find too early - before the ASP.NET Ajax client-side application object is initialized.

    Could you provide more details about your scenario? Pasting the relevant code here would be of help.

    Regards,
    Albert,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Ken Jones
    Ken Jones avatar
    41 posts
    Member since:
    Sep 2009

    Posted 03 Sep 2009 Link to this post

    Thanks for your response.

    Here is the aspx code, further help would be much appreciated.

    Ta,

    Ken

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomLink.aspx.cs" Inherits="ExtranetAdmin.CustomLink" %>

    <%@ 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></title>
        <link href="res/styles/ExAdmin.css" rel="Stylesheet" type="text/css" />
        <link href="res/customSkins/TreeView.Extranet.css" rel="stylesheet" type="text/css" />
    </head>
    <body style="background-color:#EEF7FD;">
        <form id="form1" runat="server">
            <telerik:RadScriptManager ID="RadScriptManager1" Runat="server"></telerik:RadScriptManager>
            <telerik:RadTreeView ID="uiRadTreeView" Runat="server" EnableViewState="true" EnableEmbeddedSkins="false" Skin="Extranet" OnClientDoubleClick="NodeDoubleClick" style="white-space:normal">
            </telerik:RadTreeView>
        </form>
    </body>
    </html>

    <script type="text/javascript">
        if (window.attachEvent)
        {
            window.attachEvent("onload", initDialog);
        }
        else if (window.addEventListener)
        {
            window.addEventListener("load", initDialog, false);
        }

        function getRadWindow()
        {
               if (window.radWindow)
            {
                return window.radWindow;
            }
            if (window.frameElement && window.frameElement.radWindow)
            {
                return window.frameElement.radWindow;
            }
            return null;
        }

        function initDialog()
        {
            var clientParameters = getRadWindow().ClientParameters; //return the arguments supplied from the parent page
            var link = clientParameters.html;

            var myString = clientParameters.href;
            //var search = myString.search;
            var mySplitResult = myString.split("&");

            var fileId = "";
            var nodeId = "";
            for (i = 0; i < mySplitResult.length; i++) {
                var mySecondSplitResult = mySplitResult[i].split("=");
                var paramName = mySecondSplitResult[0];
                var paramValue = mySecondSplitResult[1];

                if (paramName == "FileId") {
                    fileId = paramValue;
                }
                else if (paramName == "NodeId") {
                    nodeId = paramValue;
                }
            }

            //var form = document.getElementById("form1");
            //alert(form.childNodes.length);
            var treeview = $find("<%=uiRadTreeView.ClientID %>");        

            if (link != undefined) {
                if (link.indexOf("FileId") > 0) {
                    alert(fileId);
                    //document.getElementById('uiRadMultiPage').set_selectedIndex(2);
                }
                else if (link.indexOf("NodeId") > 0) {
                alert(nodeId);
                var node = treeview.findNodeByValue(nodeId);
                node.get_parent().expand();
                node.select();
                
                //document.getElementById('uiRadMultiPage').set_selectedIndex(1);
                }
                else {
                    //alert(clientParameters.href);
                    document.getElementById('uiTxtLink').value = clientParameters.innerHTML;
                    document.getElementById('uiTxtHref').value = clientParameters.href;
                }
            }
        }

        function NodeDoubleClick(sender, eventArgs) {
        
            var oArg = new Object();
            var node = eventArgs.get_node();
            var nodeText = node.get_text();
            var nodeId = node.get_value();

            oArg.type = "Node";
            oArg.target = "";
            oArg.linkText = nodeText;
            oArg.nodeId = nodeId;

            getRadWindow().close(oArg);
        }

        function uiBtnSelectDoc_ClientClick(sender, eventArgs) {

            var oArg = new Object();
            
            oArg.type = "Document";
            oArg.fileId = sender.fileid;
            oArg.target = "_blank";
            oArg.linkText = sender.title;
            
            getRadWindow().close(oArg);
        }

        function SubmitClick() {

            var oArg = new Object();

            oArg.type = "External";
            oArg.target = "_blank";
            oArg.linkText = document.getElementById('uiTxtLink').value;
            oArg.href = document.getElementById('uiTxtHref').value;

            getRadWindow().close(oArg);
        }
    </script>

  4. Ken Jones
    Ken Jones avatar
    41 posts
    Member since:
    Sep 2009

    Posted 03 Sep 2009 Link to this post

    Is it because I am doing the call to $find() in initDialog()????
  5. Answer
    Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 03 Sep 2009 Link to this post

    Hello Ken Jones,

    Thank you for sending your code. It seems it is case number 2. The window.load event executes before ASP.NET Ajax controls are initialized. I suggest you use the pageLoad function which is called by the ASP.NET Ajax client-side runtime when everything is initialized:

    function pageLoad() {
        initDialog();
    }

    Then remove the following code:
        if (window.attachEvent)
        {
            window.attachEvent("onload", initDialog);
        }
        else if (window.addEventListener)
        {
            window.addEventListener("load", initDialog, false);
        }


    Regards,
    Albert,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Ken Jones
    Ken Jones avatar
    41 posts
    Member since:
    Sep 2009

    Posted 03 Sep 2009 Link to this post

    Thanks, I will give it a go and let you know how it goes!
  7. Ken Jones
    Ken Jones avatar
    41 posts
    Member since:
    Sep 2009

    Posted 03 Sep 2009 Link to this post

    Thanks, it seems to work.

    Cheers,

    Ken
Back to Top