0

The Code:

ParentControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ParentControl.ascx.cs" 
Inherits="ParentControl" %>
<%@ Register Src="~/ChildControl.ascx" TagPrefix="Prefix" TagName="ChildControlTag" %>

ParentControl.ascx.cs

public partial class ParentControl : UserControl
{
   List<ASP.childcontrol_asxc> controlList = new List<ASP.childcontrol_asxc>();
   ... inside a public property setter ...
   var control = new ASP.childcontrol_ascx();  // <ABC> what the heck is this reference?
   controlList.Add(control);
   ...
}

ChildControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ChildControl.ascx.cs" 
Inherits="ChildControl" %>
<asp:Repeater ID="childRepeater" runat="server" EnableViewState="false" 
ViewStateMode="Disabled">

ChildControl.ascx.cs

public partial class ChildControl : UserControl
{
   ...

   protected void Page_Load(object sender, EventArgs e)
   {
      childRepeater.DataSource = xyz;  // <XYZ> crashes if I don't use the weird reference  
   }
}

The Problem:

I don't understand what the reference comment marked with <ABC> is all about. That's not the class I defined... but Intellisense doesn't complain. If I press F12 on it, it takes me to the <%@ Control ... %> tag at the top of ChildControl.ascx instead of the control definition in ChildControl.ascx.cs. I don't understand what this other version of the control is, why it's in the ASP namespace, or what the implications of using it instead of the control directly are. Worse, this compiles fine on my local workstation, but throws a compiler error on the TFS server we use for CI.

The Question:

Does anyone know the name for this method of using/referencing the custom User Controls, and/or have links/information I can look at to get a better handle on what this is and what the implications are? I've been unable to find anything useful via Google. While a fix/workaround would be great to have - I would still like to be able to research the technique.

Explorations:

I've tried replacing the usages with the actual class name, instead of the weird ASP.class_ascx references... it compiles fine if I do this, but unfortunately it fails at runtime. It seems like the other reference changes how it interacts with the asp.NET lifecycle - server controls defined in the aspx of the child control are null in the Page_Load() of the child control (marked <XYZ>). Under the weird ASP.control_ascx reference approach, the repeaters are properly defined when it hits the Page_Load() of the child controls, but I don't understand why.

Miscellaneous:

The project is using the Roslyn compiler on my local workstation, via the DotNetCompilerPlatform NuGet package but I believe its just using the VS/TFS 2015 built in compiler on the CI server. This might explain why TFS throws compiler errors - it tells me The type or namespace name 'usercontrol_ascx' does not exist in the namespace 'ASP' (are you missing an assembly reference?). I'm looking at configuring TFS to use the compiler from the NuGet package which will then hopefully compile - but this is still weird and I'd like to understand it.

Mir
  • 2,429
  • 1
  • 29
  • 34
  • 1
    Your code is missing a `new` keyword. – SLaks Jan 31 '18 at 20:06
  • So it is - good catch. That was just a copy/paste error... I've fixed it. – Mir Jan 31 '18 at 20:19
  • 1
    Very weird. wondering if you did a simple search of your solution for mychildusercontrol. also check for public props inside user controls. also note that a user control can have a class name that's different than the file name by doing `<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ChildControl.ascx.cs" Inherits="ChildControl" ClassName="childctrl" %>`. the *class* name (childctrl) will come up in intellisense when you work with the ASP namespace, i.e. `ASP.childctrl`. last thought would be a rebuild (not build). – wazz Jan 31 '18 at 23:22
  • @wazz Sorry, typo - it should have been `var control = new ASP.childcontrol_ascx();` not mychildusercontrol. It's the ChildControl defined in childcontrol.aspx.cs, but it's a weird reference to it. as SLaks has pointed out, it's the 'compiled' version of it... but it's still causing me issues. – Mir Feb 01 '18 at 17:22
  • 1
    It is oddly hard to find docs about this. – wazz Feb 02 '18 at 15:29
  • It's especially frustrating, because it's not just a different name to refer to the object by - it fundamentally changes the behavior of how it works (I think it lives in a different place in the page life cycle this way). – Mir Feb 02 '18 at 17:49

1 Answers1

1

This is the class that the ASPX file compiles to. It inherits your class and adds code that renders your ASPX content.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • That makes sense, though I'm still having trouble finding any documentation about it. Is it normal for it to be used directly like this? Is there some assembly reference you normally need in place that might explain why it compiles locally, but not in the CI environment? – Mir Jan 31 '18 at 20:35