views:

385

answers:

3

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?

+3  A: 

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;
    }
  }
}
Adrian Godong
A: 

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

BigBlondeViking
Asker wants this to be a user control, not a server control.
Rex M
+1  A: 

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>

James