Pass variables and access methods of User Control from CodeBehind
<p>So I have a function <code>renderWidget()</code> in <em>default.aspx.cs</em>. The idea of this is to add a User Control to a page. </p>

<pre><code>public void renderWidget(string data) {
    Control ctrl = Page.LoadControl("/widgets/widget.ascx");
    dataPanel.Controls.Add(ctrl);
}
</code></pre>

<p>This all works fine, lovely jubbly. Now in the other end, at the User Control, I have the following code in <em>widget.ascx.cs</em>:</p>

<pre><code>public class WidgetControl : System.Web.UI.UserControl
{
    public string testString = "";
    public void test() {
        Response.Write("test");
    }

}
</code></pre>

<p>The problem arises when I try to access either of the properties in the User Control. When I try to add <code>ctrl.test()</code> or <code>ctrl.testString = "test"</code> to default.aspx.cs, I get the error <strong>"'System.Web.UI.Control' does not contain a definition for 'test'".</strong> I feel like there's probably something very basic I'm missing here.</p>

<p>Any help appreciated, thanks!</p>
