从页面到Web用户控件禁用转发器中的控件

时间:2011-10-20 19:01:12

标签: asp.net repeater asp.net-4.0 webusercontrol

我有一个用户控件,我有Repeater控件,其中有两个图像按钮。

我希望能够将图像按钮的可见性设置为false。

我可以将用户控件的其他控件的可见性设置为false,如下所示......

this.Comment1.FindControl("btnAddNote").Visible = false;

...但我无法为ItemTemplate的{​​{1}}内的2图片按钮设置可见性为false。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

当您处理转发器内部的控件时,FindControl方法无法访问项目模板中的控件。为此,您必须遍历每个转发器的项目并在RepeaterItem上使用FindControl。

由于你的转发器位于用户控件内部,我建议你像这样在你的usercontrol上创建一个方法,然后从页面调用它。

//user control
public void HideNotes(){
   foreach (RepeaterItem ri in Repeater1.Items)
      ri.FindControl("btnAddNote").Visible = false;
}

//page
void btn_hide_Click(object sender, EventArgs e){
   this.Comment1.HideNotes();
}