0

I have a login control and at is nested 2 deep in a header control i.e Page --> Header Control --> Login Control. I cannot get a reference to the control on the page using FindControl. I want to be able to set the visible property of the control like

  if (_loginControl != null)
            _loginControl.Visible = false;

I ended up using a recursive FindControl method to find the nested control.

    public static Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }

        return null;
    }
Brendan
  • 1,486
  • 3
  • 19
  • 27
  • I'm not sure what you mean when you say test if it exists? Are you adding it dynamically to the page? If you have declared it in your markup then it has to have an ID and `runat="server"` so you should be able to just reference it by ID, and it should always be populated. – Josh Sep 15 '10 at 17:47

5 Answers5

0
private List<Control> GetAllNestedUserControl(Control ph)
    {
        List<Control> Get = new List<Control>();
        foreach (var control in ph.Controls)
        {
            if (control is UserControl)
            {
                UserControl uc = control as UserControl;
                if (uc.HasControls())
                {
                   Get =  GetAllNestedUserControl(uc);

                }
            }
            else
            {
                Control c = (Control)control;
                if (!(control is LiteralControl))
                {
                     Get.Add(c);
                }
            }
        }
        return Get;
    }

just call this code from you any parent page and then get any control by the following code

        List<Control> Get = GetAllNestedUserControl(ph);
        Label l = (Label)Get.Find(o => o.ID == "lblusername");
        l.Text = "changed from master";
Muneeb Zulfiqar
  • 1,003
  • 2
  • 13
  • 31
0

A good way would be to use:

Page.FindControl() 

if that yields null, the control is not there.

  • Yes, but FindControl takes the ID as string. What is the ID of a user control? – Brendan Sep 15 '10 at 17:48
  • Lots of caveats here. `FindControl` is not recursive. If the control is deeply nested this will not work. Also, they should only need to do this if the control was added dynamically. – Josh Sep 15 '10 at 17:49
  • The ID of your user control is what you set the ID to be via the Properties window (or through the declarative markup in your .aspx page). Just like how a TextBox or Button has an ID, the User Control has an ID, as well. – Scott Mitchell Sep 15 '10 at 17:50
  • So if its nested 2 controls deep should I jsut cast to the control type? – Brendan Sep 15 '10 at 18:04
  • @Zener: You could do that. You could also create a recursive FindControl method - there are plenty already out there. Here's one - http://www.west-wind.com/Weblog/posts/5127.aspx – Scott Mitchell Sep 15 '10 at 22:59
  • Thanks Scott, I had just finished rephrasing the original question when I saw your reply. I used the recursive method but had to plagarise the code, the shame! – Brendan Sep 15 '10 at 23:18
0

Try calling this.FindControl("_loginControl") or this.Page.FindControl("_loginControl").

See MSDN for method details: http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx

qbeuek
  • 4,156
  • 4
  • 19
  • 17
0

Are you needing to disable/hide the User Control from the ASP.NET page it resides on (or does the User Control exist on a master page, say)? If it's in the same page, then in your ASP.NET page's code-behind you'd do:

MyUserControlsID.Visible = false

Where MyUserControl is the ID of your User Control. To determine the ID of your User Control look at the markup of your .aspx page and you will see something like this:

<uc1:UserControlName ID="MyUserControlsID" runat="server" ... />

Happy Programming!

Scott Mitchell
  • 8,659
  • 3
  • 55
  • 71
  • Thanks Scott, I was using FindControl but not using the correct ID from the declarative code. Everyone here is correct, thanks. I will bear the caveats in mind Josh. – Brendan Sep 15 '10 at 18:01
  • If the User Control is in the ASP.NET page itself, you shouldn't need to use FindControl. You should be able to reference it from the code-behind class like you would any other Web control, i.e.: MyUserControlsID. – Scott Mitchell Sep 15 '10 at 18:10
  • Actually its a control in a control on the page so its ID is in the ascx. I do need to use FindControl. – Brendan Sep 15 '10 at 18:15
  • Or you could "reveal" the control using a public property of the parent User Control, meaning you could do: ParentUserControl.ChildUserControl from your ASP.NET page's code-behind class. But in any event, I'm glad you found a solution. :-) – Scott Mitchell Sep 15 '10 at 18:40
0

The login control, if it's registered in the markup, will also be an instance member of your codebehind page; you can refer to it from the codebehind class as if it were a normal member, using the same name you provided as the ID (I do recommend using codebehinds for most logic, instead of inlining code in the markup, BTW).

You can also use the FindControl() method of your page, which will search its control subtree for a control with a given ID. That takes longer, so I would recommend the first option unless the logic control is added dynamically and you don't always know it's there.

KeithS
  • 70,210
  • 21
  • 112
  • 164