TreeView Click on the bug fix and in-depth discussion of the event.

xiaoxiao2021-03-06  61

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:

One has a function that triggers the event is queueevent (,), see the trigger condition is not: if (Event.NewTreenodeDeIndex! = Event.NewtreenodeDex), we are now (Event.oldtreenodeIndex == Event.newtreenodeinde X), what should I do?

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.