ASP. NET Treeview check box to select all or all
(1) The Treeview control does not disclose the autopostback attribute. Therefore, to submit the check box selection, you must place a control (such as the button Web Server Control) on the page to send the information back to the server.
(2) Another method is to write JavaScript scripts on the client.
But there is a problem: Treeview is dynamically generated. When will all selected scripts be called? The answer must be to call the fully-selected Javascript script function on the onclick event on the checkbox of the root node. Then the HTML in the automatically generated page Code There is no onclick event. What should I do? The problem has been transferred. What should I do? Add one by yourself. How can I add one? When can I add one? When to add, of course, when the page is loaded (in the page_load event), that's a problem. How to add it?
The Code is as follows (add an onclick event to the root node ):
String STR = "<SCRIPT type = 'text/JavaScript 'Language = 'javascript'>
Document. getelementbyidx_x ('treeview1n0checkbox'). setattribute ('onclick', 'rec
() '); </SCRIPT> ";
Clientscriptmanager cs = page. clientscript;
CS. registerstartupscript (this. GetType (), "addonclick", STR );
Next, we will find that there is no error at all, but it cannot achieve the effect. The reason is very simple. There is no implementation of the "Rec ()" method.
The method code is as follows:
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function Rec (){
var OBJ = Document. getelementbyidx_x ('treeview1n0checkbox '); // obtain the root node element
for (VAR I = 0; I If (document. getelementsbytagname_r ("input "). item (I ). type = "checkbox") {
document. getelementsbytagname_r ("input "). item (I ). checked = obj. checked;
}}
}
</SCRIPT>
In this way, the code needs to be further optimized to achieve a more powerful effect. This is not the case.
It's hard to understand, but I have been thinking about it for two days.
(3) jquery can be used for implementation at the end, which is simpler and more powerful (all or none of the first-level nodes can be selected )!
Therefore, this method is recommended.
The Code is as follows:
<% @ Page Language = "C #" codebehind = "Treeview. aspx. cs" inherits = "Treeview. Treeview" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<SCRIPT src = "scripts/jquery-1.4.1-vsdoc.js" type = "text/JavaScript"> </SCRIPT>
<Script language = "JavaScript" type = "text/JavaScript">
$ (Document). Ready (function (){
// To avoid confusion with other elements on the page, place the Treeview control in a div with the ID of Treeview, and then
// Search for the checkbox
$ ("# Treeview table tr TDInput [type = checkbox] "). Click (
Function (){
$ ("# Treeview Div [ID =" + $ (this ). ATTR ("ID "). tostring (). replace (/checkbox/, "nodes") + "] Table tr TDInput [type = checkbox] ")
. ATTR ("checked", $ (this). ATTR ("checked "));
}
);
});
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div id = "Treeview">
<Asp: Treeview id = "treeview1" runat = "server" showcheckboxes = "all">
<Nodes>
<Asp: treenode text = "" value = "">
<Asp: treenode text = "Business College" value = "Business College"> </ASP: treenode>
<Asp: treenode text = "" value = ""> </ASP: treenode>
</ASP: treenode>
<Asp: treenode text = "city" value = "city">
<Asp: treenode text = "Jinan City" value = "Jinan City">