Author: veryhappy (wx.net)
When using the Treeview control in ASP. NET 2.0, you will find the treenode object with the checkbox control (Treeview node). If you select checkbox, the page cannot be sent back. In msdn, The Treeview. treenodecheckchanged event has a note: "When the check box of the Treeview control changes the status between two requests to the server, the treenodecheckchanged event is triggered. This allows you to provide an event processing method, that is, to execute a custom routine (such as updating the database or displaying content) each time this event occurs ).AlthoughTreenodecheckchangedEvents are triggered during sending back, but changing the check box does not cause sending back."Note that the framework itself cannot provide a checkbox sending back mechanism. In order to achieve the choice of the Set link, the author implemented a method to use JavaScript to send back, which solved this problem in disguise, although this method looks pretty bad, it can solve our actual problems to a certain extent.
The general idea is that the treenode object outputs an appended HTML object (including TD, A, inputcheckbox ......), There is no way to add a client script. Therefore, add a script to The onclick event of the Treeview control client. The script aims to determine all objects that cause the event. If it is an event caused by an inputcheckbox object, call _ dopostback to send the page back and forth. As for the background code, the idea is simple. recursively select the relevant node and set its checked attribute.
The following example shows how to select all the child nodes of the currently selected node.
Code Section:
File Treeview. aspx
Javascript:
<SCRIPT>
Function postbackbyobject ()
{
VaR o = Window. event. srcelement;
If (O. tagname = "input" & O. type = "checkbox ")
{
_ Dopostback ("","");
}
}
</SCRIPT>
CS:
Protected void page_load (Object sender, eventargs E)
{
Treeview1.attributes. Add ("onclick", "postbackbyobject ()");
}
Protected void treeviewinclutreenodecheckchanged (Object sender, treenodeeventargs E)
{
Setchildchecked (E. node );
}
Private void setchildchecked (treenode p_node)
{
Foreach (treenode _ n in p_node.childnodes)
{
_ N. Checked = p_node.checked;
If (_ n. childnodes. Count> 0)
{
Setchildchecked (_ n );
}
}
}
We hope there are better solutions, especially the method of not sending back pages.