Monday, April 21, 2008

Disabling treeview postback in ASP.NET 2.0

Sometimes you didn't need a postback when selecting a node in a treeview or you just use the treeview for displaying the data, but how can you do this when by default every treenode has an attribute value of javascript:__doPostBack in href which causes the postback.

To do this, all you have to do is set the NavigateUrl attribute to "javascript:void(0)" of the TreeNodeBinding.

<asp:TreeNodeBinding DataMember="item" Depth="1" NavigateUrl="javascript:void(0)" ValueField="name" />

Note that this only disable the postback in 2nd level of nodes as what i have set in the Depth attribute.

Works on IE and Firefox.
Attached here is the sample code.

Download Sample (8.39 kb)

6 comments:

Ali said...

tons of thanks, saved my day...

tvance said...

dude this saved my bacon! TWO YEARS LATER!! thanks!

Kovorka said...

thank you thank you thank you thank you thank you thank you!!!!
<3 you for this!

Anonymous said...

thanks a lot

Anonymous said...

or just "#"

Anonymous said...

Thanks, this helped me even after 6 years =)