Sana.91
Sana.91

Reputation: 2249

Why am i unable to access any of the label controls on my web form ASP.NET

This is my code:

   public void SetLabelVisibilityFalse()
    {
        foreach(Control cntrl in this.Page.Form.Controls)
        {
            if(cntrl is Label)
            {
                Label lbl = ((Label)cntrl);
                lbl.Visible = false;
            }
        }
    }

but i am unable to access any of the control ... it is setting NO label on the page as false.. where am i wrong?

Upvotes: 0

Views: 24

Answers (1)

Guru006
Guru006

Reputation: 26

if you are using master page than first you want to find Contentplaceholder than you can find any control of that page

 public void SetLabelVisibilityFalse()
{
    ContentPlaceHolder mycont = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
    foreach (Control cntrl in mycont.Controls)
    {
        if (cntrl is Label)
        {
            Label lbl = ((Label)cntrl);
            lbl.Visible = false;
        }
    }

Upvotes: 1

Related Questions