CSharp Datalist Data Binding With Objects – ADO.Net Database

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

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.