ASP.NET 4 Unleased - p 48

Using List Controls FIGURE Show list items with programmatic binding. LISTING /// /// Represents an item in the /// shopping cart /// public class CartItem { private int _id; public string _description; public int Id { get { return _id; } } public string Description From the Library of Wow! eBook Overview of the List Controls 445 { get { return _description; } } public CartItem(int id, string description) { _id = id; _description = description; } } void Page_Load() { if (!IsPostBack) { // Create shopping cart List shoppingCart = new List(); (new CartItem(1, “Notebook Computer”));. | 444 CHAPTER 10 Using List Controls FIGURE Show list items with programmatic binding. LISTING @ Page Language C @ Import Namespace DOCTYPE html PUBLIC - W3C DTD XHTML EN http TR xhtml11 DTD script runat server summary Represents an item in the shopping cart summary public class CartItem private int _id public string _description public int Id get return _id public string Description From the Library of Wow eBook Overview of the List Controls 445 get return -description public CartItem int id string description _id id -description description void Page_Load if IsPostBack Create shopping cart List CartItem shoppingCart new List CartItem new CartItem 1 Notebook Computer new CartItem 2 HD Plasma Television new CartItem 3 Lava Lamp Bind ListBox to shopping cart shoppingCart script html xmlns http 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 10 div form body html From the Library of Wow eBook 446 CHAPTER 10 Using List Controls In Listing the ListBox is bound to the collection in the Page_Load method. The DataTextField and DataValueField properties of the ListBox control represent properties of the CartItem class. NOTE A List control s DataTextField and DataValueField properties can refer to any public property of a class but you cannot bind a List control to a public field. Determining the Selected List Item Displaying options with the List controls is all very nice but at some point you need to determine which option a user has selected. The List controls support three properties that you can use to determine the selected list item SelectedIndex Gets or sets the index of the selected list item. SelectedItem

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
9    77    2    26-06-2024
113    105    6    26-06-2024
5    463    2    26-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.