DataList data binding with objects
<script language="C#" runat="server">
public class State {
string _name;
string _timezone;
public State(string name, string timezone) {
_name = name;
_timezone = timezone;
}
public string Name {
get { return _name; }
}
public string TimeZone {
get { return _timezone; }
}
}
protected void Page_Load(object o, EventArgs e) {
if(!IsPostBack) {
ArrayList states = new ArrayList();
states.Add(new State("Washington", "Pacific"));
states.Add(new State("Utah", "Mountain"));
datalist.DataSource = states;
datalist.DataBind();
datalist.SelectedIndex = 0;
}
}
</script>
<form runat="server">
<asp:DataList
runat="server"
id="datalist"
BackColor="tan"
RepeatDirection="Vertical"
BorderWidth="1"
BorderColor="Black"
Repeatcolumns="2"
CellSpacing="3"
CellPadding="4"
>
<SelectedItemStyle BackColor="red" >
</SelectedItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name") %> is in
<%# DataBinder.Eval(Container, "DataItem.Timezone") %>
</ItemTemplate>
</asp:DataList>
</form>
Related examples in the same category