Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3942110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:37:24+00:00 2026-05-20T00:37:24+00:00

Aloha, I have a custom control that I need to instantiate from within a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:37:25+00:00Added an answer on May 20, 2026 at 12:37 am

    Use

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

    instead of

    CustomControlClass c = new CustomControlClass();
    

    UPDATE: fix cast bug

    //LoadControl
    ASP.customcontrolclass_ascx c = (ASP.customcontrolclass_ascx)this.LoadControl("~/Controls/CustomControlClass.ascx");
    //set myValue
    c.myValue = 3;
    

    you can’t see ASP.customcontrolclass_ascx type in VS AutoComplete List but it compiled with no error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Aloha I have a VS2008 solution to which I want to add a webservice
I have a GIF image that has an alpha set, and when my site
I have a application that uses the Aero glass effect, so each pixel has
I have a custom UITableView cell that sports an Image, and a headline. I
Aloha I have a method with (pseudo) signature: public static T Parse<T>(string datadictionary) where
I have a number of RGBA pixels, each of them has an alpha component.
Im struggling a bit with the UIkeyboard, I have a datePicker, IntervalPicker some custom
I'm working with a Win32 application that needs to create a variety of custom
I made in GWT custom modal message box. It has also bottom layer. This
I want to change the background color of a custom view. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.