JS gets a method for selecting the text and value value of a node in the TreeView control _javascript Tips

Source: Internet
Author: User
In the actual project, a problem is encountered, first a new window pops up, a TreeView control is placed in a new window, and the data binding of the TreeView control is in my last essay (TreeView bound XML Data source C # code example), and now to solve, How to click a node in the TreeView, return text and value to the parent page, and close the new window.
First, add a property onclick to the TreeView in the background code to support the TreeView's client events, as follows:
Copy Code code as follows:

if (! IsPostBack)
{
TREEVIEW1.ATTRIBUTES.ADD ("onclick", "returnvalue ()");//returnvalue as JavaScript function
Bindtreeview ();
}

Now it's time to figure out how to get the selected node in the TreeView through JS, as follows:
Copy Code code as follows:

Function ReturnValue () {
var objnode = event.srcelement;
var unitid = Event.srcElement.href;
if (objnode.tagname!= "SPAN") {
Return
}
Window.opener.document.getElementById ("Txtunit"). Value = Objnode.getattribute ("innerHtml");
Window.opener.document.getElementById ("Txtunitid"). Value = Unitid;
Window.close ();
}

In fact, there is a little gimmick, because I really do not know in which attribute can get bound to the TreeView Valuefield value, so, value value I tied to the NavigateUrl, that is, write the yellow part of the code, the HTML code is as follows:
Copy Code code 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 get the text displayed on the TreeView, because if you look at the source file, you will see that the value of the TreeView's Text property is placed in span.
The green part of the code is used to backfill the parent page, but it is important to note that "txtunit" must be a client control, because if it is a server-side control, the window will report that the control does not exist at compile time.
In this way, I need the function is realized! Also, one might say that a client control is used to load backfill values in a parent page, so what if you want to use it in a server event? Also very simple, C # code is as follows:
request.form["Txtunit"]. ToString ();
But note that the "Txtunit" here is not the control ID, but the Name property!

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.