ASP.Net c# - How do I instantiate an instance of a WebUserControl from within a App_Code class?

StackOverflow https://stackoverflow.com/questions/4940446

  •  31-10-2019
  •  | 
  •  

Frage

Aloha,

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 Page 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?

I read here 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 CS0103: The name 'litTest' does not exist in the current context. 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.

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

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?

My partial class in the App_Code folder:

public partial class CustomControlClass : System.Web.UI.UserControl
{

}

My partial class in for the user control:

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";
    }
}

My user control ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControlClass.ascx.cs" Inherits="CustomControlClass" %>
<asp:Literal ID="litTest" runat="server"></asp:Literal>
TEST

My App_Code class:

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);
    }
}

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 MyClass.doWork() from the pages Load, Init and PreInit callbacks, all result in the dame thing.

Update:

As Minh Nguyen suggested I can use Ctrl.LoadControl("~/Controls/CustomControlClass.ascx"); to load the control but I can not cast it as its own type I have to assign it as a Control type:

Control c= Ctrl.LoadControl("~/Controls/CustomControlClass.ascx");

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:

c.GetType().GetProperty("myValue").SetValue(c, 1, null);

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.

I'm leaving this unanswered for a bit to see if there are any other options.

Keine korrekte Lösung

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top