The Question
I have a class 'test' which contains two public string properties p1 and p2.
Then I build an ArrayList with some newly created instances of class 'test'.
Now I want to use this ArrayList for data binding. So I tried to write: databinder.eval(container.dataitem,"p1")
But this does not work. An exception is always thrown, stating that class
'test' does not contain a property p1. Please could you explain, how databinder.eval
works with custom classes, not against a DataSet and so on.
The Answer
There are a lot of samples that show you how to data bind DataReaders or DataSets
to DataGrids, Repeaters, etc. But what happens if you want to use your own classes
within a collection? This article shows you how to do it through the following
example.
Suppose you have a Person class that look like this:
public class Person
{
int age = 0;
string name = "";
public Person()
{
}
public Person(string
name, int age)
{
this.name = name;
this.age = age;
}
public int Age
{
get { return
age; }
set
{
if
(value < 0)
throw
new ArgumentException("The age must be a positive number");
age = value;
}
}
public string Name
{
get { return
name; }
set { name =
value; }
}
}
|
Now you have a System.Collections.ArrayList class
that holds a number of Persons and you want to show them in a DataGrid. How
can you do it using the DataBind method? If you are using a DataReader or a
DataSet you could do as follows: <%# DataBinder.Eval(Container.DataItem,
"age") %> and
<%# DataBinder.Eval(Container.DataItem, "name") %>
But if your try to do it with our ArrayList collection an exception will be
throw since it can't find the property age/name in the Container.DataItem
object.
To show the person in the DataGrid you have to cast the Container.DataItem
to the class in your collection. The code below shows how to cast: <%#
((Person)Container.DataItem).Age %> and
<%# ((Person)Container.DataItem).Name %>
Since an ArrayList is not type safe you can have any object derived class in
it. You must be carefull when using this collection data binding approach. In
order to make our example more safisticated we will add a new class to our collection.
It's the Employee class that is derived from Person. The Employee class is described
below:
public class Employee : Person
{
string occupation = "";
public Employee(string
name, int age, string
occupation) : base(name, age)
{
this.occupation
= occupation;
}
public string Occupation
{
get { return
occupation; }
set { occupation
= value; }
}
} |
Suppose now that you have an ArrayList collection with Persons and Employees
and you want to show it in a DataGrid. This DataGrid contains 3 columns (name,
age and occupation). You cannot cast the Container.DataItem
to Employee because it would throw an exception since not all of the items are
Employee instances. To show the occupation to employees only items, you can
write a method that checks if the underlying object is an Employee instance
and then return its occupation:
string GetOccupation(object
o)
{
if (o is
Employee)
{
Employee emp = (Employee)o;
return emp.Occupation;
}
return "";
} |
In the DataGrid: <%# GetOccupation(Container.DataItem)
%>
That is how to data bind to Web controls using collection classes.