Is there a way around the exception you get when you use an array of structs to bind to a repeater? The repeater complains that "Id" is not a property on my struct. Technically that is correct, it is a public field.
A data presentation control should be made a data-binding with some public properties in a struct or a class. If you cannot use a struct with public properties, please change to class for experience..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RepeaterDemo
{
public struct Student
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Student(int id,string name)
{
this.id = id;
this.name = name;
}
}
//Create a data-binding source
public static class StudentSource
{
static List<Student> students = null;
static StudentSource()
{
students= new List<Student>()
{
new Student(1,"Stu1"),
new Student(2,"Stu2")
};
}
public static List<Student> GetAllStudents()
{
return students;
}
}
}
[Attention: Student is the same position as where you put "Default.aspx" file, and my result is:
I wasn't disagreeing that your approach would work, I just disagreed that it was the right approach in this case.
Some times people who are trying to bind data are just trying to bind data. They don't need more than what's necessary. A whole other class (i.e. your "StudentSource") is just entirely more than what was needed to solve this one specific binding problem.
Thanks for your answer, though - this might come in handy in the future for another issue.
russnem
Contributor
7001 Points
1389 Posts
ASPInsiders
MVP
struct as data source in Repeater
Aug 13, 2009 11:32 PM|LINK
Is there a way around the exception you get when you use an array of structs to bind to a repeater? The repeater complains that "Id" is not a property on my struct. Technically that is correct, it is a public field.
Any advice is appreciated.
andrewjboyd
Contributor
4426 Points
864 Posts
Re: struct as data source in Repeater
Aug 14, 2009 12:04 AM|LINK
You really have two options
Regards,
Andrew
================================================
Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
PeteNet
All-Star
81342 Points
11398 Posts
Re: struct as data source in Repeater
Aug 14, 2009 04:27 AM|LINK
use public properties. here's a working sample that I based off of this thread: http://forums.asp.net/p/1416076/3122334.aspx
.ASPX:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> <ItemTemplate> <%# Eval("Name") %> <br /> <div style="margin-left:10px;"> <asp:Repeater ID="Repeater2" runat="server"> <ItemTemplate> - <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem %>' /><br /> </ItemTemplate> </asp:Repeater> </div> </ItemTemplate> </asp:Repeater>.ASPX.CS:
protected void Page_Load(object sender, EventArgs e) { ArrayTest[] _oArrTest = new ArrayTest[3]; _oArrTest[0].I = 1; _oArrTest[0].Name = "Nikhil"; _oArrTest[0].Subject = new string[3]; _oArrTest[0].Subject[0] = "Math"; _oArrTest[0].Subject[1] = "Physics"; _oArrTest[0].Subject[2] = "Chemistry"; _oArrTest[1].I = 2; _oArrTest[1].Name = "Scott"; _oArrTest[1].Subject = new string[3]; _oArrTest[1].Subject[0] = "Math"; _oArrTest[1].Subject[1] = "Physics"; _oArrTest[1].Subject[2] = "Chemistry"; _oArrTest[2].I = 3; _oArrTest[2].Name = "Hanselman"; _oArrTest[2].Subject = new string[3]; _oArrTest[2].Subject[0] = "Math"; _oArrTest[2].Subject[1] = "Physics"; _oArrTest[2].Subject[2] = "Chemistry"; Repeater1.DataSource = _oArrTest; Repeater1.DataBind(); } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { ArrayTest arr = (ArrayTest)e.Item.DataItem; Repeater rpt = e.Item.FindControl("Repeater2") as Repeater; if (rpt != null) { rpt.DataSource = arr.Subject; rpt.DataBind(); } } } public struct ArrayTest { int i; public int I { get { return i; } set { i = value; } } string name; public string Name { get { return name; } set { name = value; } } string[] subject; public string[] Subject { get { return subject; } set { subject = value; } } }I could have done some more 'safe' checks but does work with some changes, and then I added the nesting
just in case you need a VB version
Peter
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: struct as data source in Repeater
Aug 19, 2009 07:11 AM|LINK
A data presentation control should be made a data-binding with some public properties in a struct or a class. If you cannot use a struct with public properties, please change to class for experience..
russnem
Contributor
7001 Points
1389 Posts
ASPInsiders
MVP
Re: struct as data source in Repeater
Aug 19, 2009 02:09 PM|LINK
Decker,
Thanks for your reply but I disagree.
I added properties to my struct (something I was not aware you could do with structs) and the repeater could bind properly.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: struct as data source in Repeater
Aug 20, 2009 01:26 AM|LINK
Well, I made an experiment to prove what I say, if you have soem strange questions, we can have a discussion (as to my limit knowledge).
This is my Default.aspx file's design:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Repeater Demo</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater runat="server" ID="rep"> <ItemTemplate> <%#Eval("Id") %> <%#Eval("Name") %> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>This is my Student file:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace RepeaterDemo { public struct Student { private int id; public int Id { get { return id; } set { id = value; } } private string name; public string Name { get { return name; } set { name = value; } } public Student(int id,string name) { this.id = id; this.name = name; } } //Create a data-binding source public static class StudentSource { static List<Student> students = null; static StudentSource() { students= new List<Student>() { new Student(1,"Stu1"), new Student(2,"Stu2") }; } public static List<Student> GetAllStudents() { return students; } } }[Attention: Student is the same position as where you put "Default.aspx" file, and my result is:
1Stu1 2Stu2]
russnem
Contributor
7001 Points
1389 Posts
ASPInsiders
MVP
Re: struct as data source in Repeater
Aug 20, 2009 06:15 AM|LINK
Decker,
I wasn't disagreeing that your approach would work, I just disagreed that it was the right approach in this case.
Some times people who are trying to bind data are just trying to bind data. They don't need more than what's necessary. A whole other class (i.e. your "StudentSource") is just entirely more than what was needed to solve this one specific binding problem.
Thanks for your answer, though - this might come in handy in the future for another issue.