[Solution] Treeview without Autopostback but with SelectedIndexChange

S

Stefan

Hi,

I developed an ASP.NET Web Application with many User Controls in it. One of
that including
the MS Treeview Control. Since i wanted the Control to track the
SelectedIndex, i implemented
a server-side eventhandler to do so.

One of the drawbacks of the Treeview Control (as others mentioned too) is
its Autopostback-
functionality (On: Postback on all events including expanding of nodes and
so on / Off: All Postbacks
off). So i searched the web for a solution, to let the Treeview Control fire
the postback only in case of
SelectedIndexChange event but not in other cases. (relevant performance
costs)

The Solution posted by Patrick Cole (Microsoft Developer Support) was
perfectly matching but
could only be used in applications implementing the TreeView Control in
Webpages (but not in
User Controls) because of the need to modify the <body> tag.

Here this Solution:
http://groups.google.com/groups?hl=...w=1&selm=Q1yaFpiPCHA.1828@cpmsftngxa08&rnum=6

I modified this solution in such way, that it can be used "everywhere" (i
tested it only in my user control), here
the code:

======================================================
public abstract class TheUserControl : System.Web.UI.WebControl
{
protected Microsoft.Web.UI.WebControls.TreeView ctlTree;

private void Page_Load(object sender, System.EventArgs e)
{
Page.RegisterClientScriptBlock("TreeEvent",
this.BuildClientsideEventhandler());
this.ctlProjectTree.Attributes.Add("onselectedindexchange",
"javascript:treeEvent();");
}

private String BuildClientsideEventhandler()
{
String nl = Environment.NewLine;
String strRef = Page.GetPostBackEventReference(ctlTree);
String strControl = this.ctlProjectTree.UniqueID.Replace(":", "_");
return "<script language=\"JavaScript\">" + nl +
"<!-- " + nl +
" function treeEvent() {" + nl +
" if (event.oldTreeNodeIndex != event.newTreeNodeIndex) " + nl +
" " + strControl + ".queueEvent('onselectedindexchange',
event.oldTreeNodeIndex + ',' + event.newTreeNodeIndex); " + nl +
" window.setTimeout('" + strRef.Replace("'","\\'") + "', 0,
'JavaScript'); " + nl +
" } " + nl +
"// --> " + nl +
"</script>";
}
}
===========================================================

You don't have to do anything additionaly..

Sincerlely,
Stefan Kals
mailto:[email protected]
http://www.css-web.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top