Skip to content
February 11, 2011 / pauldundon

Implementing INamingContainer on an ExtenderControl

The problem: You build an ExtenderControl with child controls which require postback events (eg LinkButtons). You implement INamingContainer to support this. When using your control, the base class throws an InvalidOperationException saying it cannot find your target control (“The TargetControlID of ‘…’ is not valid. A control with ID ‘…’ could not be found.”)

The cause: The ScriptManager invokes FindControl on your Extender to find the target control. If your extender is not a naming container, it delegates this call to its naming container which will usually also host the target control (for example, if your extender works with a GridView and they are both hosted in the same Panel). If your control is a naming container, it searches its children for the target control and does not find it.

Possible solution: Override FindControl as follows

    Public Overrides Function FindControl(ByVal id As String) As System.Web.UI.Control

        Dim child As Control = MyBase.FindControl(id)
        If child Is Nothing And Not NamingContainer Is Nothing Then
            Return NamingContainer.FindControl(id)
        Else
            Return child
        End If

    End Function
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: