I
'
m not sure if anyone has posted a similar solution, but this has always been a naggin problem that I wanted to solve. As you know, separating postback functionality in the TreeView control is a serious issue. With the current control, if you want to postback on the change of an index, but not on expand/collapse, you
'
re
out
of luck. The following
is
a modification of the TreeView source code that exposes addition functionality allowing
for
tighter control.
The new source code eliminates the " AutoPostBack " property and replaces it with: " AutoPostBackOnCheck " , " AutoPostBackOnSelectedIndexChange " , " AutoPostBackOnExpand " , and " AutoPostBackOnCollapse " .
You need to modify the treeview.cs source file that comes with the microsoft ie web controls.
Here are the steps to create the updated treeview control.
1 ) Comment out the " AutoPostBack " property in treeview.cs
2 ) Add the following code:
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnExpand " ),
]
public bool AutoPostBackOnExpand
{
get
{
object b = ViewState[ " AutoPostBackOnExpand " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnExpand " ] = value;
}
}
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnCollapse " ),
]
public bool AutoPostBackOnCollapse
{
get
{
object b = ViewState[ " AutoPostBackOnCollapse " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnCollapse " ] = value;
}
}
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnSelectedIndexChanged " ),
]
public bool AutoPostBackOnSelectedIndexChange
{
get
{
object b = ViewState[ " AutoPostBackOnSelectedIndexChange " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnSelectedIndexChange " ] = value;
}
}
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnCheck " ),
]
public bool AutoPostBackOnCheck
{
get
{
object b = ViewState[ " AutoPostBackOnCheck " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnCheck " ] = value;
}
}
This will add the appropriate extra properties.
3 ) Replace code in the procedure " RenderUpLevelPath " with the following:
if (Page != null )
{
string strOnExpand = " javascript: " + " if (this.clickedNodeIndex != null) this.queueEvent('onexpand', this.clickedNodeIndex); " ;
string strOnCollapse = " javascript: " + " if (this.clickedNodeIndex != null) this.queueEvent('oncollapse', this.clickedNodeIndex); " ;
string strOnCheck = " javascript: " + " if (this.clickedNodeIndex != null) this.queueEvent('oncheck', this.clickedNodeIndex); " ;
string strOnSelectedIndexChange = " javascript: " + " if (event.oldTreeNodeIndex != event.newTreeNodeIndex) this.queueEvent('onselectedindexchange', event.oldTreeNodeIndex + ',' + event.newTreeNodeIndex); " ;
if ( this .AutoPostBackOnExpand == true ) {strOnExpand = strOnExpand + Page.GetPostBackEventReference( this , "" );}
if ( this .AutoPostBackOnCollapse == true ) {strOnCollapse = strOnCollapse + Page.GetPostBackEventReference( this , "" );}
if ( this .AutoPostBackOnCheck == true ) {strOnCheck = strOnCheck + Page.GetPostBackEventReference( this , "" );}
if ( this .AutoPostBackOnSelectedIndexChange == true ) {strOnSelectedIndexChange = strOnSelectedIndexChange + Page.GetPostBackEventReference( this , "" );}
output.AddAttribute( " onexpand " , strOnExpand);
output.AddAttribute( " oncollapse " , strOnCollapse);
output.AddAttribute( " oncheck " , strOnCheck );
output.AddAttribute( " onselectedindexchange " , strOnSelectedIndexChange);
}
4 ) Compile the source and add the .dll to your project. The treeview can then PostBack separately on different events.
Ciao
The new source code eliminates the " AutoPostBack " property and replaces it with: " AutoPostBackOnCheck " , " AutoPostBackOnSelectedIndexChange " , " AutoPostBackOnExpand " , and " AutoPostBackOnCollapse " .
You need to modify the treeview.cs source file that comes with the microsoft ie web controls.
Here are the steps to create the updated treeview control.
1 ) Comment out the " AutoPostBack " property in treeview.cs
2 ) Add the following code:
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnExpand " ),
]
public bool AutoPostBackOnExpand
{
get
{
object b = ViewState[ " AutoPostBackOnExpand " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnExpand " ] = value;
}
}
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnCollapse " ),
]
public bool AutoPostBackOnCollapse
{
get
{
object b = ViewState[ " AutoPostBackOnCollapse " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnCollapse " ] = value;
}
}
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnSelectedIndexChanged " ),
]
public bool AutoPostBackOnSelectedIndexChange
{
get
{
object b = ViewState[ " AutoPostBackOnSelectedIndexChange " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnSelectedIndexChange " ] = value;
}
}
[
Category( " Behavior " ),
DefaultValue( false ),
PersistenceMode(PersistenceMode.Attribute),
ResDescription( " AutoPostBackOnCheck " ),
]
public bool AutoPostBackOnCheck
{
get
{
object b = ViewState[ " AutoPostBackOnCheck " ];
return ((b == null ) ? false : ( bool )b);
}
set
{
ViewState[ " AutoPostBackOnCheck " ] = value;
}
}
This will add the appropriate extra properties.
3 ) Replace code in the procedure " RenderUpLevelPath " with the following:
if (Page != null )
{
string strOnExpand = " javascript: " + " if (this.clickedNodeIndex != null) this.queueEvent('onexpand', this.clickedNodeIndex); " ;
string strOnCollapse = " javascript: " + " if (this.clickedNodeIndex != null) this.queueEvent('oncollapse', this.clickedNodeIndex); " ;
string strOnCheck = " javascript: " + " if (this.clickedNodeIndex != null) this.queueEvent('oncheck', this.clickedNodeIndex); " ;
string strOnSelectedIndexChange = " javascript: " + " if (event.oldTreeNodeIndex != event.newTreeNodeIndex) this.queueEvent('onselectedindexchange', event.oldTreeNodeIndex + ',' + event.newTreeNodeIndex); " ;
if ( this .AutoPostBackOnExpand == true ) {strOnExpand = strOnExpand + Page.GetPostBackEventReference( this , "" );}
if ( this .AutoPostBackOnCollapse == true ) {strOnCollapse = strOnCollapse + Page.GetPostBackEventReference( this , "" );}
if ( this .AutoPostBackOnCheck == true ) {strOnCheck = strOnCheck + Page.GetPostBackEventReference( this , "" );}
if ( this .AutoPostBackOnSelectedIndexChange == true ) {strOnSelectedIndexChange = strOnSelectedIndexChange + Page.GetPostBackEventReference( this , "" );}
output.AddAttribute( " onexpand " , strOnExpand);
output.AddAttribute( " oncollapse " , strOnCollapse);
output.AddAttribute( " oncheck " , strOnCheck );
output.AddAttribute( " onselectedindexchange " , strOnSelectedIndexChange);
}
4 ) Compile the source and add the .dll to your project. The treeview can then PostBack separately on different events.
Ciao