This is a migrated thread and some comments may be shown as answers.

Get TextBox ClientID in Javascript

8 Answers 645 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 29 Jan 2009, 01:53 PM
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.

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Feb 2009, 09:56 AM
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.
0
Nicolaï
Top achievements
Rank 2
answered on 08 Jul 2010, 12:29 PM
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;
0
Nikolay Tsenkov
Telerik team
answered on 13 Jul 2010, 12:25 PM
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
0
Nicolaï
Top achievements
Rank 2
answered on 13 Jul 2010, 12:31 PM
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ï
0
Nikolay Tsenkov
Telerik team
answered on 13 Jul 2010, 02:08 PM
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
0
Jon
Top achievements
Rank 1
answered on 08 Sep 2010, 03:42 PM
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
0
Jon
Top achievements
Rank 1
answered on 09 Sep 2010, 03:42 PM
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
0
Tomasz M.Lipiński
Top achievements
Rank 1
answered on 17 Nov 2010, 03:01 PM
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
Tags
TreeView
Asked by
Anthony
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Nicolaï
Top achievements
Rank 2
Nikolay Tsenkov
Telerik team
Jon
Top achievements
Rank 1
Tomasz M.Lipiński
Top achievements
Rank 1
Share this question
or