Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net c# - How do I instantiate an instance of a WebUserControl from within a App_Code class?
    primarykey
    data
    text
    <p>Aloha,</p> <p>I have a custom control that I need to instantiate from within a class and add to a page. My class has a function that I pass the <code>Page</code> to so i have access to the controls on the page. I'm using this method to add standard ASP controls already and everything it working as expected. My problem is that the type for the custom control is know defined?</p> <p>I read <a href="https://stackoverflow.com/questions/182984/asp-net-instantiate-a-web-user-control-in-app-code-class">here</a> to move the .cs from the control into the App_Code folder but when I do that it won't compile as it doesn't see the controls in the ascx as valid. For example I get <code>CS0103: The name 'litTest' does not exist in the current context</code>. So as they are partial classes I created a new empty partial class in the App_Code folder and left the control's .cs file alone.</p> <p>Well it compiles this way but when I add the control to the Page.controls collection, I dont see anything on the page where it should be. For example I added <code>TEST</code> to the bottom of the ascx file, it I dont see it on the page. Additionally the control has variables that I need to set that I don't have access to when using the empty partial class.</p> <p>I know that what I am trying to do works with standard controls, why can't I seem to make this work with a custom control?</p> <p><strong>My partial class in the App_Code folder:</strong></p> <pre><code>public partial class CustomControlClass : System.Web.UI.UserControl { } </code></pre> <p><strong>My partial class in for the user control:</strong></p> <pre><code>public partial class CustomControlClass : System.Web.UI.UserControl { private int _myValue= -1; public int myValue { get { return _myValue; } set { _myValue= value; } } protected void Page_Init(object sender, EventArgs e) { litTest.Text = "TEST"; } } </code></pre> <p><strong>My user control ascx:</strong></p> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControlClass.ascx.cs" Inherits="CustomControlClass" %&gt; &lt;asp:Literal ID="litTest" runat="server"&gt;&lt;/asp:Literal&gt; TEST </code></pre> <p><strong>My App_Code class:</strong></p> <pre><code>public class MyClass { public static void doWork(Page Ctrl) { CustomControlClass c = new CustomControlClass(); //c.myValue = 1; // Wont compile with this line Ctrl.Controls.Add(c); Literal l = new Literal(); l.Text = "HELLO"; Ctrl.Controls.Add(l); } } </code></pre> <p>The page output does not have the word TEST on it at all. The word HELLO shows up just fine. I've tried calling the <code>MyClass.doWork()</code> from the pages <code>Load</code>, <code>Init</code> and <code>PreInit</code> callbacks, all result in the dame thing.</p> <p><strong>Update:</strong></p> <p>As <em>Minh Nguyen</em> suggested I can use <code>Ctrl.LoadControl("~/Controls/CustomControlClass.ascx");</code> to load the control but I can not cast it as its own type I have to assign it as a Control type:</p> <pre><code>Control c= Ctrl.LoadControl("~/Controls/CustomControlClass.ascx"); </code></pre> <p>Doing this will let me add the control to the page and have it work as expected. The down side is that I have no access to any of the controls properties. To solve this i am doing this:</p> <pre><code>c.GetType().GetProperty("myValue").SetValue(c, 1, null); </code></pre> <p>This works, but I'm not sure how expensive that it to use. I wish I could just cast the control but I get an error when I try.</p> <p>I'm leaving this unanswered for a bit to see if there are any other options.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload