直接用repeater.Findcontrol是获取不到子控件的,因为子空间实际是在ItemTemplate中的,翻译一个评论:
if(Roles.IsUserInRole("admin"))
{
Button delete = (Button)rptNotering.Items[0].FindControl("btnDelete");
delete.Visible = true;
Button edit = (Button)rptNotering.Items[0].FindControl("btnEdit");
edit.Visible = true;
}
Inside a repeater are 0-many Items. .Items[x] is where x is the index of the specific itemtemplate instance.
If your repeater returns 10 rows of data, then your repeater returns an instance of ItemTemplate 10 times
and places each instance in the Items[] collection.
Your findcontrol method is not looking inside the ItemTemplate.
Hope this helps :-)
大体意思是:在repeater中有0项,Items[x] 才是制定模版列的实例,如果你的repeater有10行数据,那么你的repeater返回10次ItemTemplate 的实例,每个实例都是在item的集合众的。
所以我在事件中作了,效果还是可以,只不过这样的循环感觉不是很好,还有注意你要在repeater数据邦定后再来做如上操作,否则没效果的。。。。