DataList data binding with objects : DataList « ADO.net Database « ASP.Net






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

1.asp:datalist: repeat column, repeat directions, gridlines,
2.Bind data to asp:datalist
3.Bind data source to asp:DataList
4.datalist and ItemTemplate
5.Set the DataSource to a String array of file names
6.Bind the Hashtable to the repeater
7.LinkButton in ItemTemplate