动态生成ASP.NET按钮时要注意的一个问题

   因为领导要求在模块的顶部将分类绑定(我个人认为没什么意义,但领导说的就一定有道理),于是有一个动态绑定按钮问题出来了,代码很简单:

 

ExpandedBlockStart.gif 代码
protected   void  BindBtn()
        {
            
if  (CategoryIds.Length  >   0 )
            {
                
string [] split  =  CategoryIds.Split( ' , ' );
                
int [] cids  =   new   int [split.Length];
                
for  ( int  i  =   0 ; i  <  split.Length; i ++ )
                {
                    cids[i] 
=   int .Parse(split[i]);
                    LinkButton btn 
=   new  LinkButton();
                    btn.ID 
=   " btnCategory "   +  cids[i].ToString();
                    btn.CommandArgument 
=  cids[i].ToString();
                    CategoryController ctlCate 
=   new  CategoryController();
                    CategoryInfo infoCate 
=  ctlCate.Get(cids[i], ArticleManagerModuleID);

                    btn.Text 
=  infoCate  !=   null   ?  infoCate.Name :  " value =  "   +  cids[i].ToString();

                    btn.Click 
+=   new  EventHandler(CategoryBtnClicked);


                    phCategory.Controls.Add(btn);
                    Label lbl 
=   new  Label();
                    lbl.Width 
=   10 ;
                    lbl.Height 
=   10 ;
                    phCategory.Controls.Add(lbl);
                }
            }
        }

 

 

却在调用时一直不能触发事件,怎么也没想明白,后来发现我将BindBtn放在了if(!IsPostBack) 中,代码如下:

 

if ( ! IsPostBack) 

    
// .......
    BindBtn();

}

 

由于动态生成的控件 ,有的生存周期。在初始化里面写的话,回发时就没了,响应不了。放到外面问题解决。

 

转载于:https://www.cnblogs.com/GDLMO/archive/2010/11/22/1884889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值