title: Call function in User Control from code behind of the page
body: <p>Now this is all way simplified, but here goes:</p>
  
  <p>I have a User Control that consists only of a single *.ascx file.  The control has no code-behind: it's just a script with a few functions, like this:</p>
  
  <pre><code>&lt;%@ Control Language="VB" EnableViewState="False" ClassName="MyControlType" %&gt;
  &lt;script runat="server"&gt;
      Public Function MyFunction() As String
         return "CalledMyFunction!"
      End Function
  &lt;/script&gt;
  </code></pre>
  
  <p>That's the entire file.  I can successfully add this control to an aspx page using markup like so:</p>
  
  <pre><code>&lt;%@ Register Src="~/path/to/Control.ascx" TagPrefix="aaa" TagName="MyControl" %&gt;
  ...
  &lt;aaa:MyControl runat="server" id="MyControl1" /&gt;
  </code></pre>
  
  <p>Now what I want to do is call MyFunction from the page's code-behind, like this:</p>
  
  <pre><code>Dim someString As String = MyControl1.MyFunction()
  </code></pre>
  
  <p>Unfortunately, I can't do that. Instead, I get a compile error to the effect of "<code>'MyFunction' is not a member of 'System.Web.UI.UserControl'.</code>"</p>
  
  <p>I've also tried this:</p>
  
  <pre><code>Dim someString As String = DirectCast(MyControl1, MyControlType).MyFunction()
  </code></pre>
  
  <p>and then the compiler tells me, "<code>Type 'MyControlType' is not defined.</code>"</p>
  
  <p>I've played with this a lot, and I just can't make it work.  All efforts to cast MyControl1 to a more exact type have failed, as have other work-arounds. I suspect the problem is that the ascx file without a code-behind is unable to be compiled to an assembly but the code-behind wants to be compiled to an assembly and therefore the compiler gets confused about what type the control is.</p>
  
  <p>What do I need to do to be able to call that function?</p>
  
  <p>[edit]<br>
  So I'm just gonna have to add code-behind for the user control.  It's what I wanted to do anyway.  I'd still like to know how to do this without needing one, though.</p>