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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:11:11+00:00 2026-06-13T07:11:11+00:00

I want to add usercontrols to my page based on a XML: <?xml version=1.0

  • 0

I want to add usercontrols to my page based on a XML:

<?xml version="1.0" encoding="utf-8" ?>
<Fields>
<Group name="Main" text="Innhold">
<Field type="TextBox" name="Name" text="Navn"></Field>
</Group>
</Fields>

The usercontrol looks like this TextBox.ascx:

<div class="fieldWrapper">
<asp:Label runat="server"><%=Label %></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" />
</div>

I do a LoadControl based on the type attribute in the xml. Like: LoadControl(type + “.ascx”):

var fields = from x in element.Elements("Field") select new
{
type = x.Attribute("type").Value,
name = x.Attribute("name").Value,
text = x.Attribute("text").Value,
                                    };
foreach (var field in fields)
{
var control = LoadControl("~/UserControls/FieldControls/" + field.type + ".ascx");
pnl.Controls.Add(control);
}
FieldsHolder.Controls.Add(pnl);

I want to pass the text attribute from the xml to the Label in TextBox.ascx.
Like so: ctrl.Label = field.text
I know if i cast the control to the correct type I can do that, but i dont know the type.
Can i use reflection for this some way?

  • 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-13T07:11:12+00:00Added an answer on June 13, 2026 at 7:11 am

    I assume all your UserControls share the same properties like ‘Label’. I would have created an interface like below

    public interface IUserControl
    {
        string Label { get; set; }
    }
    

    Here is the implementation of UserControl

    CODE

    public partial class TextBox : System.Web.UI.UserControl, IUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        private string _label;
        public string Label
        {
            get { return _label; }
            set { _label = value; }
        }
    }
    

    You can now load the control and set property like below

    foreach (var field in fields)
    {
       var control = LoadControl("~/UserControls/FieldControls/" + field.type + ".ascx");
       (control as IUserControl).Label = field.text;
       pnl.Controls.Add(control);
    }
    

    I hope it helps you achieve what you wanted..

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

Sidebar

Related Questions

I am in a situation where I want to add usercontrols in a panel.
I want add my custom sidebar next right column all page. Please check this
I want to add a user control to my pages without page post backs.
I have a UserControl that I want to add at runtime to my current
I have 10 custom usercontrol's with ID's usercontrol1, usercontrol2, usercontrol3......usercontrol10. I want to add
I want add UIGestureRecognizerDelegate to UIWebView,but failed. if [self.view addsubView:webView]; So UIWebView is ok,but
I want add new Feed item on entity persist and update. I write this
I want add label to every row in tableview at right side of the
I have a website built with ASP.NET (3.5) and want add some level of
I have a list of string values that I want add to a hashtable

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.