Andrey
Andrey

Reputation: 21275

How do I bind an IEnumerable to a Repeater in aspx markup?

I want to output all defined routes onto a page without any code behind, so I need to bind RouteTable.Routes to a Repeater in markup. How do I do that - <asp:Repeater> only has DataSourceID value available in markup, not DataSource. I assume I need to declare a DataSource for RouteTable.Routes and then provide its ID to the Repeater, but how do I do that?

Again, I need a solution without any code-behind, declarative only.

I'm using asp.net 4.0 (not MVC)

Upvotes: 3

Views: 3261

Answers (2)

deostroll
deostroll

Reputation: 11975

You'd need to do some minimal plumbing to make this work. Here is a sample for a gridview. You can adapt the technique for a repeater too.

DataSource Object Class:

public class MyObjectDataSourceEntity
{
    [DataObjectMethod(DataObjectMethodType.Select)]
    public DataTable GetSomeRecords()
    {
        DataTable names = new DataTable();
        DataColumn FirstName = names.Columns.Add();
        FirstName.DataType = typeof(string);
        FirstName.ColumnName = "FirstName";
        DataColumn LastName = names.Columns.Add();
        LastName.DataType = typeof(string);
        LastName.ColumnName = "LastName";

        DataRow row = names.NewRow();
        row[0] = "Arun";
        row[1] = "Jayapal";
        names.Rows.Add(row);
        row = names.NewRow();
        row[0] = "Namith";
        row[1] = "Chandran";
        names.Rows.Add(row);
        return names;
    }

You need to include the System.ComponentModel namespace.

Markup:

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        DataSourceID="ObjectDataSource1">
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetSomeRecords" 
        TypeName="ObjectDataSourceDemo.MyObjectDataSourceEntity">
    </asp:ObjectDataSource>

Note: I've added a datatable here because the gridview only understand that object when it comes to sorting right out-of-the-box. So you don't have to write extra code. Otherwise you use any IEnumerable object. But you'd have to write your own logic for sorting. Nothing to do with repeaters, but I thought I'd mention this.

Now you don't need to call DataBind() on anything!!!

PS: Once you've written code for your entity, build your project first. After this you can rely on your designer for adding the ObjectDataSource control onto the page. The wizard automatically detects supportable objects in the project, but you need to build solution first.

Upvotes: 1

Joe Alfano
Joe Alfano

Reputation: 10289

The asp.net Repeater control does have a DataSource property, and you can set it in the aspx markup (see screen shot below):

Screen shot of Repeater markup intellisense

Typically, what is done is to set the DataSource property to a databinding expression, which gets evaluates when the Page.DataBind() method is called.

<asp:Repeater ID="foo" DataSource = '<%# GetRoutes() %>' runat=server ></asp:Repeater>

I think you will still need some code on the page to call Page.DataBind() to initiate databinding on the aspx page.

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind();
    }

This code could be put in the code-behind or, since you do not want to use code-behind, in a server code block in the aspx page. But I believe it has to be called via C# code in a page event handler. I do not believe this can be done in a purely declarative manner.

Upvotes: 6

Related Questions