For a while, I have a friend who asked me: "How do you get a return server? When you click on a node?" I gave him the following functions and told him that TreeView's AutoPostBack must open (value true):
Private void TreeView1_selectedIndexchange (Object Sender, Microsoft.Web.ui.WebControls.TreeViewSerectEventArgs E)
{
Treenode node = new treenode ();
Node = TreeView1.GetNodeFromindex (TreeView1.SelectedNodeIndex);
String sindex;
SINDEX = TreeView1.selectedNodeIndex;
IF (Sindex.indexof (".")> = 0)
{
Sindex = sindex.substring (0, sindex.lastIndexof ("."));
}
INITTREE (Node, Sindex);
}
Afterwards, in a few days, he asked: "Why can't you click the node that has been selected? How to solve? Herd a happiness ........."
In fact, this is a bug of TreeView. SelectedIndexchange is a change event of a node index. If a node has been selected, then it can't trigger the event, how to solve it?
Let's take a look at the code generated by the TreeView control in the front desk:
Don't worry, we give it an OnClick event to call the function is OK, pay attention to the parameters inside, otherwise it will be missed. Look at my code first:
Add in the background Page_Load area:
TreeView1.attributes ["onclick"] = "JavaScript: if (this.clickednodeIndex! = Null) this.queueevent ('OnSelectedIndexchange', '' ' this.clickednodeIndex";
Emphase: The IF (this.clickedNodeIndex! = NULL) must be, otherwise you will trigger the event like the TreeView in addition to the node, and the function can not find the index value of the node.
discuss in depth:
The background code of .NET sends it to the client after compiling the server, generates the corresponding script, and its return is started by the front event, let us see what it produces in the front desk.
Function __dopostback (evenettarget, eventargument) {
Var theform;
IF (Window.navigator.Appname.tolowerCase (). Indexof ("Netscape")> -1) {
Theform = Document.Forms ["form1"];
}
Else {
Theform = Document.form1;
}
Theform .__ eventTarget.Value = eventTarget.split ("$"). Join (":");
Theform .__ Eventargument.Value = Eventargument
Theform.submit ();
}
// ->
script>
Seeing that there is no, it generates two hidden input, what is it used? Tell you, one of them is the ID of the control of the control of the trigger event, and the other is the parameters used to temporarily, the front desk event will pass the two parameters to the __dopostback () function, and What is really playing is this __dopostback () function, if we give it a call to the event, such as: TextBox1.attributes ["onclick"] = "__dopostback (button1, '')", then it can not only Return to server processing, you can also call the unique functions of other controls. So what can you do to change yourself, but I want to explain it, this __dopostback () function is not generated every background compiling, pull out a treeview or DataGrid it will, if it is not urgent, We can add it in the background.