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 604917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:05:23+00:00 2026-05-13T17:05:23+00:00

I found this extremely useful article on creating instances of ASP.NET UserControls in .ASHX

  • 0

I found this extremely useful article on creating instances of ASP.NET UserControls in .ASHX Handlers by Scott Guthrie.

Unfortunately I now have a requirement now where I need to create an instance of a UserControl which has a dependency on the parent page. This being the case, I can’t use the same method to create the UserControl.

Can anyone suggest how I might go about this? What is involved in creating an instance of the Parent page and then getting the contents of the UserControl it contains?

Just for reference & to be specific on the type of depenendency I’m dealing with, the UserControl is a panel that exists on a main Factsheet page. Depending on the user’s configuration preferences, different controls are dynamically generated in the Factsheet’s base class FactsheetBuilderPage, and included on the page.

The UserControl has properties, such as:

public DateTime EffectiveDate
{
    get
    {
        return ((FactsheetBuilderPage)this.Page).EffectiveDate;
    }
}

public Site ConfiguredSite
{
    get { return ((FactsheetBuilderPage)this.Page).SiteConfiguration; }
}

public ConfiguredFund Fund
{
    get
    {
        return ((FactsheetBuilderPage)this.Page).Fund;
    }
}

which reference the FactsheetBuilderPage class, which means I need to create a FactsheetBuilderPage class for it to reference.

Ideally I’d be able to resolve this issue without having to modify the existing code base, because refactoring this will be a real pain!!

Thanks guys

Dave

  • 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-13T17:05:23+00:00Added an answer on May 13, 2026 at 5:05 pm

    Ok, so the solution turned out to be changing the UserControl’s properties to be automatic, instead of just having a getter that referrs to the parent page.

    public ConfiguredFund Fund { get; set; }
    
    public DateTime EffectiveDate { get; set; }
    
    public Site ConfiguredSite { get; set; }
    

    Now it means that I have to explicitely assign values to these properties when the user control is being instantiated. This allows me to create an instance of this UserControl anywhere without the need for the parent page, as long as I can supply its required data.

    then in my Handler, create an instance of the UserControl and assign the properties it needs:

    private MyUserControl GetUserControl(int id)
    {
        MyUserControl myUc = GetUserControl("Controls/MyUserControl.ascx");
    
        myUc.Fund = new ConfiguredFund(id);
        myUc.EffectiveDate = DateTime.Now;
        myUc.ConfiguredSite = siteManager.GetSiteById(2);
    
        return myUc;
    }
    

    then to get the rendered html, I use a slightly modified version of Scott Guthrie’s code:

    public static string RenderView(MyUserControl viewControl, object data)
    {
        Page pageHolder = new Page();
    
        if (data != null)
        {
            Type viewControlType = viewControl.GetType();
            FieldInfo field = viewControlType.GetField("Data");
    
            if (field != null)
            {
                field.SetValue(viewControl, data);
            }
            else
            {
                throw new Exception("View file doesn't have a public Data property");
            }
        }
    
        pageHolder.Controls.Add(viewControl);
    
        StringWriter output = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, output, false);
    
        return output.ToString();
    }
    

    Thanks Guys

    Dave

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

Sidebar

Related Questions

This is my first question, but I've already found this site extremely helpful, so
So I found this thread that was extremely helpful in traversing an array diagonally.
I have found the rand_s function, part of the CRT, to be extremely useful
I've found the ThreadStatic attribute to be extremely useful recently, but makes me now
Found this while reading the Neo4j manual, specifically here , I found the sentence:
Found This: tyty stack, Social Icons not working with Infinite Scrolling on Wordpress I'm
found this little code snippet that seems to do what i want, but im
Found this Multimap containing pairs? , but it is not much help How would
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can

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.