JavaScript to obtain the text and value of the selected node of the Treeview Control)

Source: Internet
Author: User
Javascript obtains the text and value of the selected node of the Treeview control.

In the actual project, a problem occurs. First, a new window is displayed, with a Treeview control in the new window. The data of the Treeview control is bound to my previous article (Treeview is bound to the XML data source C #CodeAs mentioned in the example), the solution is to click a node in the Treeview, return text and value to the parent page, and close the new window.

First, add the onclick attribute for the Treeview in the background code to support client events of the Treeview. The Code is as follows:

If (! Ispostback)
{
Treeview1.attributes. Add ("onclick", "returnvalue ()"); // returnvalue is a JavaScript function
Bindtreeview ();
}

Now we need to solve how to obtain the Node Selected in the Treeview through Js. The script is as follows:

Function returnvalue (){
VaR objnode = event. srcelement;
VaR unitid = event. srcelement. href;
If (Objnode. tagname! = "Span"){
Return;
}
Extends extends opener.doc ument. getelementbyid ("txtunit"). value = objnode. getattribute ("innerhtml ");
Extends extends opener.doc ument. getelementbyid ("txtunitid"). value = unitid;
Window. Close ();
}

In fact, I used some tricks here, because I really don't know which attribute can obtain the value of valuefield bound to the Treeview, So I bound the value to navigateurl together, that is the reason for writing the yellow part of the Code. The HTML code is as follows:

<Asp: Treeview id = "treeview1" runat = "server" expanddepth = "1">
<Databindings>
<Asp: treenodebinding textfield = "name" valuefield = "value" navigateurlfield = "value" selectaction = "NONE"/>
</Databindings>
</ASP: Treeview>

The red part of the code is used to obtain the text displayed on the Treeview. If you view the source file, you will find that the text attribute value of the Treeview is placed in the span.

Some green code is used to backfill the parent page, but note that "txtunit" must be a client control, because if it is a server control, the pop-up window will report that the control does not exist during compilation.

In this way, the functions I need are implemented! In addition, some people may say that the client control is used to load the backfilling value on the parent page. What should I do if I want to use it in server events? The C # code is as follows:

Request. Form ["txtunit"]. tostring ();

Note that the "txtunit" here is not the Control ID, but the name attribute!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.