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.”
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:
Code Behind for the page using the Usercontrol:
Somewhere on the page you are inserting your Usercontrol with something like:
And of course the ObjectDataSource:
FindControlRecursive is a simple Extension Method: