Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET UserControl Does Not Initialize Child Controls

Tags:

.net

asp.net

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!

like image 861
Omer van Kloeten Avatar asked Oct 28 '08 13:10

Omer van Kloeten


2 Answers

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...

like image 58
Omer van Kloeten Avatar answered Nov 08 '22 18:11

Omer van Kloeten


In my case the reason was the Resharper 7.1 added incorrect @Register directive at the top of aspx - instead of this desired row:

<%@ Register Src="~/Controls/Hello/Hello.ascx" TagName="Hello" TagPrefix="p" %>

i got wrong one:

<%@ Register TagPrefix="p" Namespace="MyNamespace.WebApp.Controls" Assembly="MyApp.Web" %>
like image 35
vladimir Avatar answered Nov 08 '22 18:11

vladimir