Accessing array data with Repeater

Discussion in 'ASP .Net' started by Roshawn, Aug 6, 2004.

  1. Roshawn

    Roshawn Guest

    Hi,

    I have a Repeater control that is databound to an array. This array
    contains data regarding products (ie. Name, Price, Manufacturer, etc.).

    I am wondering how do I display data in a control that is contained in the
    Repeater's ItemTemplate?
    Here's my example code:

    Sub BindToArray()
    'code for the array omitted here
    Me.Repeater1.DataSource = products
    Me.Repeater1.DataBind()
    End Sub

    <asp:Repeater>
    <ItemTemplate>
    <!-- say that I wanted to bind to the Name field of the array
    <asp:Label id=Label1 Text=<%#
    Container.DataItem("Name")%>></asp:Label>
    </ItemTemplate>
    </asp:Repeater>

    Would this code work?

    Roshawn
    Roshawn, Aug 6, 2004
    #1
    1. Advertising

  2. Roshawn

    Scott Allen Guest

    Hi Roshawn,

    If you expose the fields you want to display as public properties then
    one way to reach them is with the DataBinder.Eval method.

    For example, imagine an array of objects of the following class class
    (in C# but gives you the idea):

    class Department
    {
    public Department(string name, string description)
    {
    this.name = name;
    Description = description;
    }

    public string Name
    {
    get { return name; }
    set { name = value; }
    }

    public string Description
    {
    get { return description; }
    set { description = value; }
    }

    protected string name;
    protected string description;

    }

    Then you can display them in a repeater item template using:

    <%# DataBinder.Eval(Container.DataItem, "Name") %>

    HTH,

    --
    Scott
    http://www.OdeToCode.com

    On Fri, 6 Aug 2004 10:22:40 -0500, "Roshawn" <>
    wrote:

    >Hi,
    >
    >I have a Repeater control that is databound to an array. This array
    >contains data regarding products (ie. Name, Price, Manufacturer, etc.).
    >
    >I am wondering how do I display data in a control that is contained in the
    >Repeater's ItemTemplate?
    >Here's my example code:
    >
    >Sub BindToArray()
    > 'code for the array omitted here
    > Me.Repeater1.DataSource = products
    > Me.Repeater1.DataBind()
    >End Sub
    >
    ><asp:Repeater>
    > <ItemTemplate>
    > <!-- say that I wanted to bind to the Name field of the array
    > <asp:Label id=Label1 Text=<%#
    >Container.DataItem("Name")%>></asp:Label>
    > </ItemTemplate>
    ></asp:Repeater>
    >
    >Would this code work?
    >
    >Roshawn
    >
    Scott Allen, Aug 6, 2004
    #2
    1. Advertising

  3. Roshawn

    Roshawn Guest

    Thanks Scott for your .NET wisdom. That helps a lot!!!

    Roshawn
    Roshawn, Aug 6, 2004
    #3
  4. Roshawn

    Scott Allen Guest

    Ooh, that sample class was ugly. I was experimenting with fields
    versus properties. Please ignore the goofy looking destructor - I'm
    glad the big concept came through.

    Best of luck!

    --s

    On Fri, 06 Aug 2004 14:18:48 -0400, Scott Allen
    <bitmask@[nospam].fred.net> wrote:

    >Hi Roshawn,
    >
    >If you expose the fields you want to display as public properties then
    >one way to reach them is with the DataBinder.Eval method.
    >
    >For example, imagine an array of objects of the following class class
    >(in C# but gives you the idea):
    >
    >class Department
    >{
    > public Department(string name, string description)
    > {
    > this.name = name;
    > Description = description;
    > }
    >
    > public string Name
    > {
    > get { return name; }
    > set { name = value; }
    > }
    >
    > public string Description
    > {
    > get { return description; }
    > set { description = value; }
    > }
    >
    > protected string name;
    > protected string description;
    >
    >}
    >
    >Then you can display them in a repeater item template using:
    >
    ><%# DataBinder.Eval(Container.DataItem, "Name") %>
    >
    >HTH,


    --
    Scott
    http://www.OdeToCode.com
    Scott Allen, Aug 6, 2004
    #4
  5. Roshawn

    Scott Allen Guest

    Oh man, I meant goofy looking CONstructor - not destructor. It's
    Friday but I don't think I've had anything to drink yet!

    --s

    On Fri, 06 Aug 2004 17:01:43 -0400, Scott Allen
    <bitmask@[nospam].fred.net> wrote:

    >Ooh, that sample class was ugly. I was experimenting with fields
    >versus properties. Please ignore the goofy looking destructor - I'm
    >glad the big concept came through.
    >
    >Best of luck!
    >
    >--s
    >


    --
    Scott
    http://www.OdeToCode.com
    Scott Allen, Aug 7, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    835
  2. Replies:
    0
    Views:
    2,857
  3. Replies:
    1
    Views:
    7,001
    Karl Seguin
    Aug 10, 2005
  4. Replies:
    1
    Views:
    15,280
    cbDevelopment
    Nov 13, 2005
  5. uncensored
    Replies:
    0
    Views:
    786
    uncensored
    Oct 25, 2006
Loading...

Share This Page