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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:33:38+00:00 2026-05-23T18:33:38+00:00

I have a user control (don’t want to create custom control) in this control

  • 0

I have a user control (don’t want to create custom control) in this control i have a listbox.
Now i want to user Datasource property which can be use as a datasource for this listbox.

<UC:CustomList ID="list" CustomDataSource="objectDataSource1" runat="server" />

In control:

 public object CustomDataSource
    {
        get
        {
            return this.checkComboBox.DataSourceID;
        }

        set
        {
            this.checkComboBox.DataSource = value;
        }            
    }

It give runtime error “Cannot create an object of type ‘System.Object’ from its string representation ‘objectDataSource1’ for the ‘CustomDataSource’ property.”

  • 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-23T18:33:39+00:00Added an answer on May 23, 2026 at 6:33 pm

    After re-examening your question I think I understand what you ask for.

    Since you are using the ID of the ObjectDataSource you need to find that control and get the SelectMethod property. Then you need to invoke that method to get the data to be presented. This is something you really don’t want to do because it leads to a lot more problems, like not being able to set the CustomDataSource property in the code front since Page.Controls isn’t populated at that stage so you cannot find the ObjectDataSource control. You can however set the property in code behind in Page_Load.

    Here is an example of how you could do it:

    Code Behind for the Usercontrol:

    public partial class MyUserControl : System.Web.UI.UserControl
    {
        private string customDataSource;
        public string CustomDataSource
        {
            get { return customDataSource; }
            set
            {
                customDataSource = value;
                var ctrl = (ObjectDataSource) this.Page.FindControlRecursive(customDataSource);
                if (ctrl == null) return;
    
                var m = this.Page.GetType().GetMethod(ctrl.SelectMethod);
                if (m == null) return;
    
                var data = m.Invoke(this.Page, BindingFlags.InvokeMethod | BindingFlags.Public, null, null, null);
    
                checkComboBox.DataSource = data;
                checkComboBox.DataBind();
            }
        }
    }
    

    Code Behind for the page using the Usercontrol:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                myCtrl.CustomDataSource = "objectDataSource1";
        }
    
        public object GetData()
        {
            return new List<string> { "1", "2", "3" };
        }
    }
    

    Somewhere on the page you are inserting your Usercontrol with something like:

    <uc:MyUserControl runat="server" id="myCtrl"></uc:MyUserControl>
    

    And of course the ObjectDataSource:

    <asp:ObjectDataSource runat="server" ID="objectDataSource1" SelectMethod="GetData"></asp:ObjectDataSource>
    

    FindControlRecursive is a simple Extension Method:

    public static class ExtensionMethods
    {
        public static Control FindControlRecursive(this Control control, string id)
        {
            if (control == null) return null;
            if (control.ID == id) return control;
    
            foreach (Control c in control.Controls)
            {
                var found = FindControlRecursive(c, id);
                if (found != null) return found;
            }
    
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control and I want to create a property of type
i have user control, which i render on several views. i want to show
I have a user control which is essentially a main menu. I can place
I have a user control that exposes a property of type ImageSource. I want
I have a view user control that can post form. This control can be
I have a custom user control and have overridden its Font property. When I
I have an ascx user control with and Entity Framework IQueryable property like this:
I have a user control which contains a number of child controls. I want
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have a user control ( gallery.ascx ) and I want to use the

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.