Get TextBox ClientID in Javascript

9 posts, 0 answers
  1. Anthony
    Anthony avatar
    25 posts
    Member since:
    Oct 2008

    Posted 29 Jan 2009 Link to this post

    Hi,

    I plan to use a treeview with Checkbox enabled and put a textbox with each node. When checkbox checked, the textbox will be visible, otherwise textbox will be hidden. This will be done in javascript for set textbox.style.display and textbox.style.visibility.

    I have no problem to setup treeview with textboxes, but my question is how can I get textbox in client side javascript. I try to use node.findControl, but I can't get correct textbox clientid. When first create treeview in server side, I use following code. 

    RadTreeNode n = new RadTreeNode();
            TextBox textBox = new TextBox();
            Label label = new Label();
            n.Controls.Add(label);
            n.Controls.Add(textBox);
            n.Attributes.Add("TextBoxID",textBox.ClientID);

    in javascript 

    function OnClientNodeCheckedEventHandler(sender, eventArgs)
        {
            var node = eventArgs.get_node()
    if(node.get_checked())
    {
    var txtID = node.get_attributes().getAttribute("TextBoxID");
    var textbox = node.findControl(txtID);
                if (textbox!=null)
                {
                    textbox.style.display="block";
                    textbox.style.visibility="visible";
                }

    }
        }

    but the problem is that textbox always return null. Can someone help me out?

    I realy appreciate it.

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Feb 2009 Link to this post

    Hi Anthony,

    Checkout the article which explains about how to access the controls inside the treeview templete. I hope this will help you.
    Accessing Controls Inside Templates

    Thanks,
    Shinu.
  3. Nicolaï
    Nicolaï avatar
    160 posts
    Member since:
    Apr 2008

    Posted 08 Jul 2010 Link to this post

    Hmm, same problem here...
    It returns "null" whenever I try findControl in javascript...

    var tree=$find("<%= myTree.ClientID %>");
    var node=tree.findNodeByValue(myvalue);
    var a=node.findControl("abc");
    alert(a); <<<< null
    return false;
  4. Nikolay Tsenkov
    Admin
    Nikolay Tsenkov avatar
    734 posts

    Posted 13 Jul 2010 Link to this post

    Hello Nicolai,

    I have just checked this out and it works fine for me.
    I tested my code with the specified version and it successfully gets reference to controls inside the template.
    Here is the markup of the TreeView:
    <telerik:RadTreeView runat="server" ID="RadTreeView1" OnClientLoad="onLoad">
        <Nodes>
            <telerik:RadTreeNode Text="Node">
                <NodeTemplate>
                    <telerik:RadTextBox runat="server" ID="textBox" Text="FoundMe!" />
                </NodeTemplate>
            </telerik:RadTreeNode>
        </Nodes>
    </telerik:RadTreeView>
    Here is the java script:
    function onLoad(sender) {
        var tree=sender;
        var node=tree.get_nodes().getNode(0);
        var textBox=node.findControl("textBox");
        alert(textBox.GetTextBoxValue());
    }

    Hope this is going to help you in resolving what the problem really is!


    Regards,
    Nikolay Tsenkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Nicolaï
    Nicolaï avatar
    160 posts
    Member since:
    Apr 2008

    Posted 13 Jul 2010 Link to this post

    Hello & thanks,

    The problem was solved in a support ticket.
    My conclusion was that the documentation ommited to mention that findControl only works with other radcontrols (and not standard controls).
    (..And I spent a LOT of time trying to findControl on a regular textbox)

    So I switched the textbox (standard) into a radtextbox, as you did, and it works again..!

    Best regards,
    Nicolaï
  6. Nikolay Tsenkov
    Admin
    Nikolay Tsenkov avatar
    734 posts

    Posted 13 Jul 2010 Link to this post

    Hello Nicolai,

    The standard asp:TextBox doesn't have a client object. That's why our method, doesn't retrieve a reference to it - it simply doesn't exist.

    Glad that I could help you!


    Regards,
    Nikolay Tsenkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Jon
    Jon avatar
    813 posts
    Member since:
    Oct 2008

    Posted 08 Sep 2010 Link to this post

    Hi Nikolay,

    What would the work around for this be if you have a label being used (rather than a radtextbox)?  I went looking for RadLabel but then remembered that it is only in WinForms not AJAX. 

    Regards,

    Jon
  8. Jon
    Jon avatar
    813 posts
    Member since:
    Oct 2008

    Posted 09 Sep 2010 Link to this post

    OK figured out the work around in case anyone has the same issue in the future.

    var uxIssuedByLabel = document.getElementById('uxRadTreeView_i' + currentNode.get_index() + '_uxIssuedBy')

    where currentNode is the current node in the tree. 

    Cheers,

    Jon
  9. Tomasz M.Lipiński
    Tomasz M.Lipiński avatar
    71 posts
    Member since:
    Nov 2009

    Posted 17 Nov 2010 Link to this post

    Hi,

    OK, but this works for the first level of nodes. Each level adds its own "_i<n>" element to the id.

    If you need the value of only one control of the template you can put this value at the server side to the <node>.Text attribute and then - retrieve it using the <node>.get_text() method.

    Regards
    Tomasz
Back to Top