Question

I'm trying to build an ASP.net user control that uses a Repeater to iterate over a number of items in an ObjectDataSource that I need to pass in to the user control. I'm not sure how to pass the object data source in though. Any one know how to do this?

Was it helpful?

Solution

You can create a property in the user control and pass it to the repeater.

public class CustomUserControl
{
  private Repeater repeater;

  public ObjectDataSource DataSource
  {
    get
    {
      return this.repeater.DataSource;
    }
    set
    {
      this.repeater.DataSource = value;
    }
  }
}

OTHER TIPS

Below are the rough steps to do this (untested).

  • List make your usercontrol a databound control. Take a look at this article to see an example http://geekswithblogs.net/mnf/articles/92205.aspx.

  • in the page that is consuming your usercontrol set the DataSourceId property declaratively or in code to your object data source.

    <uc1:YourUserControl DataSourceId="YourObjectDataSourceID"></uc1:YourUserControl>

  • List item Bind your repeater to the internal DataSourceId property via a declarative binding expression.

    <asp:repeater DataSourceId='<%# DataSourceId %>'></asp:repeater>

If you make you control inherit from CompositeDataBoundControl

[ToolboxData("<{0}:TopNav runat=server></{0}:TopNav>")]
public class TopNav : CompositeDataBoundControl

you can assign the DataSourceID to it.

<uc1:TopNav ID="YUITopNav1" runat="server" DataSourceID="ObjectDataSource1"  />

then in you control you implement

    protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
    {
        this.Repeater1.DataSource = dataSource;
        this.Repeater1.DataBind();
    }

Where the dataSource is data coming from your ObjectDataSource

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top