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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:09:34+00:00 2026-05-29T05:09:34+00:00

I have a set of user controls on a page that are dynamically loaded

  • 0

I have a set of user controls on a page that are dynamically loaded based on condition to run a variety reports (the condition driver). Each control has one or more properties exposed that will be used to get data from my database query. Because the controls vary for each report I wrote a procedure to access the appropriate control’s property by name so I can send it to the database query in the code behind (C#). I got it all setup to access the public property like this:

stringVal = userControl.Attributes[stringName].ToString();

and it is telling me that I need to new up an object. I don’t understand how I need to access that property dynamically by string name. In my immediate window I can see the property I want; but, it is not an “Attribute” as control.Attributes.Count = 0. So, how do I need to set this up properly so I can access it by string name? Do I need to decorate the property with something?

Thank you in advance.

  • 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-29T05:09:35+00:00Added an answer on May 29, 2026 at 5:09 am

    Below, I’ve written a wrapper class you can use to set/get public fields or properties of the wrapped class by string name.

    First, let’s look at how you could use it on a class that had the public fields or properties StartDate and SocialSecurityNumber.

    // First create the instance we'll want to play with
    MyUserControl myControlInstance = new MyUsercontrol();
    // MyUserContol has two properties we'll be using
    // DateTime StartDate
    // int SocialSecurityNumber
    
    // Now we're creating a map to facilitate access to the properties
    // of "myControlInstance" using strings
    PropertyMap<MyUserControl> map = 
                 new PropertyMap<MyUserControl>(myControlInstance);
    
    // Since the map is directed toward "myControlInstance"
    // this line is equivalent to:
    // myControlInstance.StartDate = Datetime.Now;
    map.Set<DateTime>("StartDate", DateTime.Now);
    
    // This line is equivalent to:
    // ssn = myUsercontrol.SocialSecurityNumber;
    int ssn = map.Get<int>("SocialSecurityNumber");
    

    And now on to how it’s implemented:

    public class PropertyMap<T>
    {
        readonly T Instance;
    
        public PropertyMap(T instance)
        {
            Instance = instance;
        }
    
        public U Get<U>(string PropertyName)
        {
            // Search through the type's properties for one with this name
            // Properties are things with get/set accessors
            PropertyInfo property = typeof(T).GetProperty(PropertyName);
            if (property == null)
            {
                // if we couldn't find a property, look for a field.
                // Fields are just member variables, but you can only
                // manipulate public ones like this.
                FieldInfo field = typeof(T).GetField(PropertyName);
                if (field == null)
                    throw new Exception("Couldn't find a property/field named " + PropertyName);
                return (U)field.GetValue(Instance);
            }
            return (U)property.GetValue(Instance, null);
        }
    
        public void Set<U>(string PropertyName, U value)
        {
            // Search through the type's properties for one with this name
            PropertyInfo property = typeof(T).GetProperty(PropertyName);
            if (property == null)
            {
                // if we couldn't find a property, look for a field.
                FieldInfo field = typeof(T).GetField(PropertyName);
                if (field == null)
                    throw new Exception("Couldn't find a property/field named " + PropertyName);
                field.SetValue(Instance, value);
                return;
            }
            property.SetValue(Instance, value, null);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have set up a login page that verifies the user's credentials, and
Have a page that adds controls dynamically. Control state is being retrieved from database
I have a set of categories that a user can choose from. Each category
I have a page that has 3 used controls that are dynamically added to
I have UserControls containing other controls. I want that if I set a Foreground
I have set permission to allow anonymous user to create page. but when the
i have some javascript in the head of a page that controls an image
How to load a user control dynamically in a page? I have a page
I have a page in my WP7 app that I build dynamically. I create
I have a ASP.NET page with 2 user controls registered. The first one has

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.