.net – ASP.NET UserControl Does Not Initialize Child Controls

asp.netnet

Inside my page, I have the following:

<aspe:UpdatePanel runat="server" ID="updatePanel">
    <ContentTemplate>
        <local:KeywordSelector runat="server" ID="ksKeywords" />
    </ContentTemplate>
</aspe:UpdatePanel>

The KeywordSelector control is a control I define in the same assembly and local is mapped to its namespace.

The control is made up of several other controls and is defined as such:

<%@ Control Language="C#" AutoEventWireup="true"
            CodeBehind="KeywordSelector.ascx.cs"
            Inherits="Keywords.KeywordSelector" %>

and has quite a few server controls of its own, all defined as members in the .designer.cs file.

However, during no part of the control's lifecycle does it have any child control objects nor does it produce HTML:

  1. All of the members defined in the .designer.cs file are null.
  2. Calls to HasControls return false.
  3. Calls to EnsureChildControls do nothing.
  4. The Controls collection is empty.

Removing the UpdatePanel did no good. I tried to reproduce it in a clean page with a new UserControl and the same thing happens.

I am using ASP.NET over .NET Framework 3.5 SP1 with the integrated web server.

What am I missing here?

Update #1: Following Rob's comment, I looked into OnInit and found that the UserControl does not detect that it has any child controls. Moreover, CreateControlCollection is never called!

Best Answer

Well, I've found the problem(s):

  1. User Controls, as opposed to Custom Controls must be registered one-by-one in the web.config file. Do this:

    <add tagPrefix="local" tagName="KeywordSelector" src="~/KeywordSelector.ascx" />

    instead of:

    <add tagPrefix="local" namespace="Keywords" assembly="Keywords" />

  2. You should never place a WebControl in the same directory as the Control that is using it. This is downright silly. Read about it here.

Thanks for the help. Now if only I could mark my own answer as the answer...

Related Topic