ASP.NET Repeater DataBind Custom Object

Suppose we wish to databind a List:

        //Or any kind of list...
        List ints = new List() { 1, 2, 3, 10, 19 };
        Repeater1.DataSource = ints;
        <asp:Repeater ID="Repeater1" runat="server">
                        <%# Container.DataItem %>

Or suppose we have a custom object:

        public class myFooBarCollection{
                public class FooBar{
                        public int myFooBarNumber = 0;
                        public String myFoorBarText = "";

                public int somefield;
                public List foobarcollection = new List();
        protected void Page_Load(object sender, EventArgs e){
                Repeater1.DataSource = (new myFooBarCollection()).foobarcollection;
        <asp:Repeater ID="Repeater1" runat="server">
                        Index: <%# Container.ItemIndex %>
                        Item: <%# ((myFooBarCollection.FooBar)Container.DataItem).myFooBarText %>

Databinding to Custom Objects
Not much has changed. Note that in this example, myCustomObjectCollection is a collection of a class that has fields theTextFieldName and theValueFieldName that have get and set methods (ie, public int theValueFieldName {get; set;}).

myASPRadioButtonList.DataSource = myCustomObjectCollection;
myASPRadioButtonList.DataTextField = "theTextFieldName";
myASPRadioButtonList.DataValueField = "theValueFieldName";

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s