0

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 Answers 3

3

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;
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wondering if this could be made more flexible by making the property data type an Object as Repeater.DataSource is
Yes. (11 more chars to go...)
1

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>

Comments

0

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

1 Comment

Asker wants this to be a user control, not a server control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.