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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:18:55+00:00 2026-05-16T20:18:55+00:00

I load the content of some User Controls into my page with the use

  • 0

I load the content of some User Controls into my page with the use of jQuery. So I have this function that extracts the content from my User Control, and it works like a charm.

    public string GetObjectHtml(string pathToControl)
    {
        // Create instance of the page control
        Page page = new Page();

        // Create instance of the user control
        UserControl userControl = (UserControl)page.LoadControl(pathToControl);

        //Disabled ViewState- If required
        userControl.EnableViewState = false;

        //Form control is mandatory on page control to process User Controls
        HtmlForm form = new HtmlForm();

        //Add user control to the form
        form.Controls.Add(userControl);

        //Add form to the page
        page.Controls.Add(form);

        //Write the control Html to text writer
        StringWriter textWriter = new StringWriter();

        //execute page on server
        HttpContext.Current.Server.Execute(page, textWriter, false);

        // Clean up code and return html
        string html = CleanHtml(textWriter.ToString());

        return html;
    }

But I would really like to send in some parameters into my User Control when I create it. Is this possible, and how can i do that?

I can see that LoadControl() can take some parameters with object[] parameters but im really not sure on how to use it, help much appreciated!

  • 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-16T20:18:55+00:00Added an answer on May 16, 2026 at 8:18 pm

    You can implement an interface on your usercontrol for the appropriate params.

    public interface ICustomParams
    {
        string UserName { get; set; }
        DateTime SelectedDate { get; set; }
    }
    

    implement the interface in the usercontrol, like this

    public partial class WebUserControl : System.Web.UI.UserControl , ICustomParams
    {
        public string UserName { get; set; }
        public DateTime SelectedDate  { get; set; }
    }
    

    then load your control:

      UserControl userControl = (UserControl)page.LoadControl(pathToControl);
    

    Acces the control via the interface

      ICustomParams ucontrol = userControl as ICustomParams;
      if(ucontrol!=null)
      {
           ucontrol.UserName = "henry";
           ucontrol.SelectedDate = DateTime.Now;
      }
    

    Done,

    you can add multiple interfaces for multiple purposes there.
    If the usercontrol hasn’t the interface implemented, the if statement will avoid using it

    But if you really cannot access the usercontrols and you know “a bit” of the properties you want to set and what type they are try a more dynamic way with reflection:

    load the usercontrol:

        UserControl userControl = (UserControl)Page.LoadControl(@"~/WebUserControl.ascx");
    

    get the properties of the loaded usercontrol:

        PropertyInfo[] info = userControl.GetType().GetProperties();
    

    loop trough it:

        foreach (PropertyInfo item in info)
        {
            if (item.CanWrite)
            {
                 switch (item.Name)
                 {
                     case "ClientName"
                         // A property exists inside the control": //
                         item.SetValue(userControl, "john", null); 
                         // john is the new value here
                     break;
                 }
            }
        }
    

    I would only encourage you this if you CANNOT access the usercontrols and there are dozens of them with lots and lots of variable properties per usercontrol. (It can get very ugly, slow and not fail-safe)

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

Sidebar

Related Questions

i use this script to load content of some div, inside another div: $(function()
I'm using jQuery UI Tabs to load some content via Ajax when the user
I'm loading in some HTML content like this: $('#scores').load('scores.php #scores'); Note that It is
I have multiple user controls loading asynchronously on my page, using pagemethods. Some dropdownlists
I have a base class for some kind of user controls, and in that
All, I'm trying to load some content into a fancybox. I'd like to load
I am using the jquery ajax api so load in some content, the code
Let's say I do this: $(#content).load(...); Within what I'm loading some javascript is included:
I have an ASP.NET web app that uses a single page, but makes some
We have some code that is making use of the new postMessage functionality in

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.