解决办法:
你放在onitemdatabound中处理,并且判断itemtype为edit的时候才做就可以实现你的目标了
原因:
在edit这个事件中,edititemtemplate里的控件其实还没建立
他的前提条件是要数据绑定以后,并且要通过oneditcommand事件才后才能被建立,这也就是为什么update事件又可以如常操作的原因了
类似的代码如这样
private void datagrid1_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
listitemtype itemtype = e.item.itemtype;
if (itemtype == listitemtype.edititem)
{
checkboxlist chk = (checkboxlist)e.item.findcontrol("chkclienttype");
这个就可以达到你的要求了,这个也应该是一个比较标准且完美的处理方式。
关于这个问题,我的朋友飞刀他有另外的一些解决方法,下面我引用他的文章,也可以给你做个参考
(2)findcontrol方法的问题
问题:
大家都知道所有控件集合都存在有一个findcontrol方法,一般最常用的地方就是datagrid对item中控件的操作。这是一个很好用的方法,可以让我们迅速地找到我们想要的控件,但是他也是我遇到过的最不稳定的方法。
在item中使用这个方法,一般不会出现什么问题,但是在datagrid、datalist的各种事件中这个方法经常是找不到控件!!datagrid还好一点,datalist的事件中发生的情况就惨不忍睹,100%的找不到控件!!这个控件是活生生存在的,使用controls集合中是可以发现这个控件的。这个问题我在beta2下就已经发现了,原以为微软会在正式版本中更正,不知道是没有人提出呢?还是没有发现,正式版中依然这样。
开始我以为findcontrol这个方法没有写好,我就自个重写了这个方法,但是当我高兴地去用我自个写的方法时,发现传回来的值还是null!!!现在也就只有一个解释了,那就是.net环境中对control类型的支持还是不稳定的。
解决方法:
即然通过编写方法传回值的方法搞不定,那么就只有用最原始的方法,在本函数内,直接列举controls集合中的控件,直到找到这个控件为止。
private void showquestion_itemdatabound(object sender, system.web.ui.webcontrols.datalistitemeventargs e)
{
//当返回值为control类型,经常出现空值
foreach(control cl in e.item.controls)
{
if(cl.clientid.indexof("optionaltd1") != -1 || cl.clientid.indexof("optionaltd2") != -1)
{
foreach(control clx in cl.controls)
{
if(clx.clientid.indexof("oplbl1") != -1 || clx.clientid.indexof("oplbl2") != -1)
{
if(((label)clx).text == "")
{
((htmltablecell)cl).innerhtml = "";
}
}
}
}
}
希望能对你有所帮助
