0

I try to access the MainContentBlock control from the aspx, but unable to do so.

In the aspx file I have registered both controls:

<uc3:ContentBlock ID="MainContentBlock" runat="server" DynamicParameter="id" DefaultContentID="3951" /></uc3>

<uc3:childshow ID="Childshow" runat="server"/></uc3>

In the code behind for child.ascx

If Me.Parent.Page.FindControl("MainContentBlock") IsNot Nothing AndAlso Me.MainContentBlock.Item.Id = 4357 Then

...

But the error says BC30456: 'MainContentBlock' is not a member of 'child'.

It's almost like the ".parent" part did not work.

However, If I try the following:

If Me.Parent.MainContentBlock IsNot Nothing AndAlso Me.MainContentBlock.Item.Id = 4357 Then

...

It will bring up the error "BC30456: 'MainContentBlock' is not a member of 'System.Web.UI.Control'.

and seems it at least recognized the .parent part again.

confused... please help, thanks.

KP.
  • 13,218
  • 3
  • 40
  • 60
eastboundr
  • 1,857
  • 8
  • 28
  • 46

3 Answers3

1

It's because you're trying to reference MainContentBlock as a property of the child control. When you use Me.MainContentBlock, Me refers to the child control.

You just need to use FindControl, and properly reference the found control:

Dim myBlock As ContentBlock = TryCast(Me.Parent.FindControl("MainContentBlock"), ContentBlock)

If myBlock IsNot Nothing Then
    'do things with myBlock
End If
KP.
  • 13,218
  • 3
  • 40
  • 60
  • Hi, but I have added .Parent.FindControl() ? I thought this would access the parent aspx file? Sorry still confused... – eastboundr Mar 29 '12 at 19:32
  • FindControl may find the control, but that doesn't change the reference or attempted reference to Me.MainContentBlock. – KP. Mar 29 '12 at 19:35
  • no problem. I added some sample code. In general you need to try to find the control and assign it to a variable. Then access it as needed. Sample isn't tested but general idea is there. Don't know what type the MainContentBlock is. Assumed `ContentBlock` – KP. Mar 29 '12 at 19:39
0

Depending on where the control is located on the page, you may need to find it recursively, but in a simple situation you would just do this:

var pnl = Page.FindControl("MainContentBlock") as Panel; //or whatever it is
if (pnl != null)
{
    //your code here
}

Here's a recursive method if you need it:

public Control FindControlRecursive(string controlID, Control parentCtrl)
{
    foreach (Control ctrl in parentCtrl.Controls)
    {
        if (ctrl.ID == controlID)
            return ctrl;
        FindControlRecursive(controlID, ctrl);
    }
    return null;
} 

And you would call it like this:

var pnl ((PageName)Page).FindControlRecursive("MainContentBlock") as Panel;
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • Hi James, Thanks, I think I manage to let the child find the control in aspx. However, I also want to access the property of that user control, such as: If Me.Parent.FindControl("MainContentBlock").EkItem IsNot Nothing Then.... Where EkItem is a property of the control. I could not access EkItem at this point since it gives me the error: BC30456: 'EkItem' is not a member of 'System.Web.UI.Control'. Anything I did wrong? thanks. – eastboundr Mar 29 '12 at 19:47
0

FindControl works but the pain is that what you're looking for can be higher then just at parent level. Here is a handy method I use:

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

    foreach (Control ctl in root.Controls)
    {
        Control foundCtl = FindControlRecursive(ctl, id);

        if (foundCtl != null)
            return foundCtl;

    }
    return null;
}
Maciej
  • 7,871
  • 1
  • 31
  • 36
  • Thanks, I think I found the control. But do you know how could I also access its property? For example: Me.Parent.FindControl("MainContentBlock").EkItem Where EkItem is a property of the user Control. The above line gives me an error : BC30456: 'EkItem' is not a member of 'System.Web.UI.Control'. Any ideas? Thanks – eastboundr Mar 29 '12 at 19:50
  • To answer previous comment: type casting – Maciej Mar 29 '12 at 21:24