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

  • SEARCH
  • Home
  • 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 7763199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:36:09+00:00 2026-06-01T14:36:09+00:00

It seems I still don’t get generics … I want a general function to

  • 0

It seems I still don’t “get” generics … I want a general function to load a user control which inherently calls Page.LoadControl() but I am getting the error above when trying to make it work.

Here is a mock up of the code I use to load the control:

MyControl Ctrl = MyUtilClass.LoadControl(Page, "MyControl");

and then in MyUtilClass:

internal static T LoadControl<T>(Page P, string ControlName)
{
    return (T)P.LoadControl(String.Format("~/{0}{1}.ascx", WebGlobals.cControlDir, ControlName));
}

I am obviously doing something wrong but my understanding was the compiler would look at the type of var I am trying to assign the result of this function to and be able to cast the result as that type.

  • 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-06-01T14:36:10+00:00Added an answer on June 1, 2026 at 2:36 pm

    I think you need to constrain your generic function to Controls only, otherwise T is too generic for your purposes.

    internal static T LoadControl<T>(Page P, string ControlName)
        where T : System.Web.UI.Control
    {
        ...
    }
    

    EDIT

    You could do this but I’m not sure its worth the extra complexity over the non generic approach in Daniel Hilgarth’s answer.

    internal static void LoadControl<T>(Page P, 
                                         string ControlName
                                         out T control)
        where T : System.Web.UI.Control
    {
        control = (T)P.LoadControl(
                       String.Format("~/{0}{1}.ascx",
                                        WebGlobals.cControlDir, 
                                        ControlName));
    }
    

    calling like this

    MyControl myControl;
    LoadControl(page, "MyControl", myControl);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that also the latest android SDK tools still don't properly support testing
EDIT: $query->where('&DiacriticalGrave;value&DiacriticalGrave; = ?', $number); It seems that does the job. I still don't
I have read the documentation, but I still don't understand which classloader I should
I am releasing things but the memory seems to still be there according to
I have read a lot about this issue but mine still seems to be
I can't believe that the following statement seems to be still true So, I
This question seems to be asked freqeuently over the internet but I still can't
I am still getting started with Unity, and have what seems to be a
This seems like a basic question, but it's still bugging me. Why doesn't MyObject
I've been looking for hosting services and I still don't know what kind of

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.