Operations on checkbox in the Treeview of ASP. net2.0

Source: Internet
Author: User

Reprinted from: http://blog.163.com/k_doll/blog/static/78450130200921453734942/

 

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 ). Although the treenodecheckchanged event is triggered during sending back, changing the check box does not cause sending back ." Note that the framework itself does not 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 backgroundCodeThe 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:
Server method:

Treeview. js
  1   < Script >
  2  
  3   Function Postbackbyobject ()
  4  
  5 {
  6  
  7 VaR O = Window. event. srcelement;
  8  
  9 If (O. tagname =   " Input "   && O. Type =   " Checkbox " )
10  
11 {
12  
13 _ Dopostback ( "" , "" );
14  
15 }
16  
17 }
18  
19 </ Script >

 

Treeview. aspx. CS
1 Protected   Void Treeviewappstreenodecheckchanged ( Object Sender, treenodeeventargs E)
2 {
3   If (E. node. childnodes. Count >   0 )
4 {
5 For ( Int I =   0 ; I < E. node. childnodes. Count; I ++ )
6 {
7 E. node. childnodes [I]. Checked = E. node. checked;
8 If (E. node. childnodes [I]. childnodes. Count >   0 )
9 {
10 For ( Int J =   0 ; J < E. node. childnodes [I]. childnodes. Count; j ++ )
11 {
12 E. node. childnodes [I]. childnodes [J]. Checked = E. node. childnodes [I]. checked;
13 }
14 }
15 }
16 }
17 }

 

The client determines whether to select a parent node and select a subnode at the same time:
Add onclick = ";" to the Treeview of the Client ";"
Client method:

Treeview. js
  1 < Script Type = " Text/JavaScript " Language = " Javascript " >
  2 Function ontreenodechecked ()
  3 {
  4 VaR ele = Window. Event . Srcelement;
  5 If (Ele. Type = ' Checkbox ' )
  6 {
  7 VaR childrendivid = Ele. Id. Replace ( ' Checkbox ' , ' Nodes ' );
  8 VaR Div = Document. getelementbyid (childrendivid );
  9 If (Div = Null ) Return ;
10 VaR checkboxs = Div. getelementsbytagname ( ' Input ' );
11 For (VAR I = 0 ; I < Checkboxs. length; I ++ )
12 {
13 If (Checkboxs [I]. Type = ' Checkbox ' )
14 Checkboxs [I]. Checked = Ele. Checked ;
15 }
16 }
17 }

 

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.