Select a Treeview node in ASP. NET 2.0

Source: Internet
Author: User

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.

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.