Show list items with programmatic binding. : ListBox « Data Binding « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    public class CartItem
    {
        private int _id;
        public string _description;

        public int Id
        {
            get { return _id; }
        }

        public string Description
        {
            get { return _description; }
        }

        public CartItem(int id, string description)
        {
            _id = id;
            _description = description;
        }
    }

    void Page_Load()
    {
        if (!IsPostBack)
        {
            List<CartItem> shoppingCart = new List<CartItem>();
            shoppingCart.Add(new CartItem(1, "A"));
            shoppingCart.Add(new CartItem(2, "B"));
            shoppingCart.Add(new CartItem(3, "C"));

            lstShoppingCart.DataSource = shoppingCart;
            lstShoppingCart.DataBind();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Programmatic DataBinding</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:ListBox
        id="lstShoppingCart"
        DataTextField="Description"
        DataValueField="Id"
        Runat="server" />

    </div>
    </form>
</body>
</html>








19.20.ListBox
19.20.1.Bind DataSet to ListBox
19.20.2.Show list items with programmatic binding.
19.20.3.ListBox Control data binding with asp:SqlDataSource
19.20.4.Selecting multiple list items (set SelectionMode to Multiple)