Jeevan Bhatt
Jeevan Bhatt

Reputation: 6101

How to set DataSource property in.aspx file of a user control?

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."

Upvotes: 0

Views: 3529

Answers (3)

VinayC
VinayC

Reputation: 49165

As Justin pointed out, correct implementation would be

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

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

Imp: Also make sure that wherever you are setting this property, you are not assigning ToString of actual data-source (which you must be doing for earlier version to work).

EDIT: From your comment, what you need is DataSourceID property i.e.

public string CustomDataSourceID
     {
            get
            {
                return this.checkComboBox.DataSourceID;
            }

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

Now, you can use this property in your mark-up to associate data source id declaratively.

Upvotes: 2

Sani Huttunen
Sani Huttunen

Reputation: 24385

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;
    }
}

Upvotes: 0

Saanch
Saanch

Reputation: 1844

Change your code to

public object CustomDataSource //<<<Changed to object from string
{
    get
    {
        return this.checkComboBox.DataSourceID;
    }

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

in your code mention the following also.

<uc1:lstControl id="lstControl1" runat="server" enabled="true"  CustomDataSource="datasourceName"/>

Upvotes: 0

Related Questions