views:

61

answers:

4

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;
    }
A: 

A good way would be to use:

Page.FindControl() 

if that yields null, the control is not there.

CodeMonkey
Yes, but FindControl takes the ID as string. What is the ID of a user control?
Zener Diode
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
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
So if its nested 2 controls deep should I jsut cast to the control type?
Zener Diode
@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
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!
Zener Diode
A: 

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
A: 

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
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.
Zener Diode
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
Actually its a control in a control on the page so its ID is in the ascx. I do need to use FindControl.
Zener Diode
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
A: 

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