Separating AutoPostBack Functionality TreeView control

博客指出TreeView控件中分离PostBack功能是个难题,当前控件无法在索引改变时PostBack而在展开/折叠时不PostBack。通过修改TreeView源代码,消除“AutoPostBack”属性,添加新属性,替换部分代码并编译,可使TreeView在不同事件中单独PostBack。
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值