Josh Mein
Josh Mein

Reputation: 28665

Parent.FindControl() not working?

I have a page that has an iframe

From one of the pages within the iframe I want to look back and make a panel on the default page invisible because it is overshadowing a popup

I tried using Parent.FindControl but it does not seem to be working. I am positive I have the right id in the findcontrol because I used Firebug to inspect the panel and I copied the id from there

Does anyone know what I am missing?

Upvotes: 1

Views: 7384

Answers (4)

cllpse
cllpse

Reputation: 21727

Parent document:

<body>
    <input type="text" id="accessme" value="Not Accessed" />
    ...
</body>

Document in iframe:

<head>
    ...
    <script type="text/javascript">
        function setValueOfAccessme()
        {
            window.parent.document.getElementById("accessme").value = "Accessed";
        }
    </script>
</head>
<body onload="setValueOfAccessme();">
</body>

The document inside the iframe accesses the document object on the window object on load, and uses the getElementId() function to set the value of the input inside the body of the parent document.

Upvotes: 2

Sean Gough
Sean Gough

Reputation: 1711

Alternatively here's a more helpful find control routine...

Public Shared Function MoreHelpfulFindControl(ByVal parent As UI.Control, ByVal id As String) As UI.Control
    If parent.ID = id Then Return parent
    For Each child As UI.Control In parent.Controls
        Dim recurse As UI.Control = MoreHelpfulFindControl(child, id)
        If recurse IsNot Nothing Then Return recurse
    Next
    Return Nothing
End Function

Upvotes: 0

jhuffaker
jhuffaker

Reputation: 621

I didn't completely follow your problem, but I'll take my best shot.

It sounds like you have an ASP.NET page, that has an iframe in it that refers to another ASP.NET page, and in that page that was requested by the iframe you want to modify the visibility of the item contained in the page that contains the iframe.

If my understanding of your problem is correct, then you have some somewhat nasty problems here.

  1. What's actually happening at the browser level is the first page gets loaded, and that page has an iframe in it that is making a second request to the server.
  2. This second request can't FindControl your control, because it isn't in the same page, and isn't alive during that request.

So you have some alternatives here:

  1. Get rid of the iframe and use a panel. This will put them both in the same request, and able to find each other.
  2. (Additionally) When you do this you are going to want to use Page.FindControl() not Parent.FindControl() as the FindControl method just searches through the Control's child control collection, and I presume your control will be somewhere else on the page.
  3. On the client side in the iframe you could use some javascript code to access the outer page's DOM, and set the visibility of it there.

Upvotes: 3

troelskn
troelskn

Reputation: 117615

For starters, FindControl isn't a function in Javascript.

Upvotes: 0

Related Questions