asp.netrepeaterasp.net-4.0webusercontrol

Disable controls in repeater from page to web user control


I have a user control in which I have Repeater control and inside this there are two image button.

I want to be able to set the visibility to false for the image buttons.

I am able to set visibility to false for the other controls of the user control like this...

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

...but am unable to set the visibility false for the 2 image button inside ItemTemplate of Repeater.

How can I do that?


Solution

  • When you're dealing with controls inside of a repeater the FindControl method is not able to access controls in the item template. To do this you have to loop through each of the repeater's items and use FindControl on the RepeaterItem.

    Since your repeater is inside the user control I'd suggest making a method on your usercontrol like this, and calling that from the page.

    //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();
    }