DataList data binding with objects

Submitted by:Jhon Brain

Date added:12 February, 2012

Category:ASP

ASP code example for DataList data binding with objects

Tags: data binding

Code Snippet:

<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>
 
 

Comments