When ASP. NET 2.0 uses the TreeView control, it finds the TreeNode object with the CheckBox control (Treeview node). If you select CheckBox, the page cannot be sent back. In MSDN, The TreeNodeCheckChanged event has a note: "A TreeNodeCheckChanged event is triggered when the check box of the TreeView control is sent to the server twice to change the status. 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 ). Although the TreeNodeCheckChanged event is triggered during sending back, changing the check box does not cause sending back ." The framework itself does not provide a CheckBoX sending mechanism.
Even if you click the checkbox button, it will not cause a PostBack request. In this way, the checkbox will be automatically sent back.
However, when building a tree, there is no way to define an event on treenode. we add this event to the Treeview.
TreeView1.Attributes. Add ("onclick", "postBackByObject ()"); // register a client event
Code
1 <script type = "text/javascript">
2 function postBackByObject ()
3 {
4 var o = window. event. srcElement;
5 // determine whether the stimulated object is checkbox. If yes, the _ doPostBack ("", "") is triggered ("","");
6 // because the checkbox generated by the foreground is "<input type = 'checkbox'./>"
7 // so the judgment is as follows:
8 if (o. tagName = "INPUT" & o. type = "checkbox ")
9 {
10 _ doPostBack ("", ""); // Note: __dopostback is composed of two underscores "_"
11}
12}
13 </script>
In. net, when all server controls are submitted to the server, the _ dopostback ("", "") function is called.
The first parameter is the ID of the control you want to submit to the server, and the second parameter is the event parameter.
The _ dopostback () function is not generated every time it is compiled in the background. It can be used to pull a Treeview or DataGrid. If not, you can add it in the background.
The prototype of _ dopostback () is as follows:
Code
1 <script type = "text/javascript">
2 <! --
3 var theForm = document. forms ['form1 '];
4 if (! TheForm ){
5 theForm = document. form1;
6}
7 function _ doPostBack (eventTarget, eventArgument ){
8 if (! TheForm. onsubmit | (theForm. onsubmit ()! = False )){
9 theForm. _ EVENTTARGET. value = eventTarget;
10 theForm. _ EVENTARGUMENT. value = eventArgument;
11 theForm. submit ();
12}
13}
14 // -->
15 </script>
Other controls can also call this event. For example, textbox1.attributes ["onclick"] = "_ dopostback (button1,") "can return not only server processing, but also special functions of other controls.
The backend CS code is as follows:
Code
1 # check box status of region cascade Parent and Child Nodes
2
3 protected void TreeView_TreeNodeCheckChanged (object sender, TreeNodeEventArgs e)
4 {
5 SetChildChecked (e. Node );
6 // determine whether the root node is used
7 if (e. Node. Parent! = Null)
8 {
9 SetParentChecked (e. Node );
10}
11}
12
13 /// <summary>
14 // set the Status of the Child Node Based on the parent node status
15 /// </summary>
16 /// <param name = "parentNode"> </param>
17 private void SetChildChecked (TreeNode parentNode)
18 {
19 foreach (TreeNode node in parentNode. ChildNodes)
20 {
21 node. Checked = parentNode. Checked;
22 if (node. ChildNodes. Count> 0)
23 {
24 SetChildChecked (node );
25}
26}
27}
28
29 private void setparentchecked (treenode childnode)
30 {
31 treenode parentnode = childnode. parent;
32 If (! Parentnode. Checked & childnode. Checked)
33 {
34 int ichecks = 0;
35 foreach (treenode node in parentnode. childnodes)
36 {
37 if (node. Checked)
38 {
39 ichecks ++;
40}
41}
42 if (ichecks = parentNode. ChildNodes. Count)
43 {
44 parentNode. Checked = true;
45 if (parentNode. Parent! = Null)
46 {
47 SetParentChecked (parentNode );
48}
49}
50}
51 else if (parentNode. Checked &&! ChildNode. Checked)
52 {
53 parentNode. Checked = false;
54}
55}
56
57 # endregion
Note: each time a Page_Load event is returned, it is the first event to be executed.
Source:
1. http://www.cnblogs.com/jerry-chen/archive/2009/04/15/1436571.html
2. http://www.cnblogs.com/bobxie85/archive/2008/07/08/1238481.html