In the actual project, I encountered a problem. First, a new window popped up. A TreeView control was placed in the new window. The data binding of the TreeView control was in my previous essay (TreeView Binding XML Data Source C# Code Example) As mentioned, what needs to be solved now is how to click a node in the TreeView, return the Text and Value to the parent page and close the new window.
First, add the onclick attribute to TreeView in the background code to support the client event of 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 get the selected node in TreeView through js. The script is 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();
}
Actually used here A little trick, because I really don’t know in which attribute I can get the value of the ValueField bound to the TreeView, so I tied the Value value to the NavigateUrl, which is why I wrote the yellow part of the code. The Html code is as follows:
The red part of the code is used to get the text displayed on the TreeView, because if you check the source file, you will find 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 should be noted that "txtUnit" must be a client-side control, because if it is a server-side control, the pop-up window will report that the control does not exist when compiling.
In this way, the function I need is realized! In addition, some people may say that the client control used to load the backfill value in the parent page is what to do if it is used in a server event? It’s also very simple. The C# code is as follows:
Request.Form["txtUnit"].ToString();
But please note that the "txtUnit" here is not the control ID, but the name attribute!