LISTING 10.3 ProgrammaticDataBinding.aspx /// /// Represents an item in the /// shopping cart /// public class CartItem { private int _id; public string _description; public int I
Trang 1LISTING 10.3 ProgrammaticDataBinding.aspx
<%@ 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”>
/// <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
FIGURE 10.3 Show list items with programmatic binding
Trang 2{
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>();
shoppingCart.Add(new CartItem(1, “Notebook Computer”));
shoppingCart.Add(new CartItem(2, “HD Plasma Television”));
shoppingCart.Add(new CartItem(3, “Lava Lamp”));
// Bind ListBox to shopping cart
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>
Trang 3In Listing 10.3, 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—Gets the first selected list item
SelectedValue—Gets or sets the value of the first selected list item
For example, the page in Listing 10.4 enables you to select an item from the DropDownList
control and display the value of the selected item’s Text property (see Figure 10.4)
FIGURE 10.4 Selecting an item from a DropDownList control
Trang 4LISTING 10.4 SelectMovie.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
protected void btnSelect_Click(object sender, EventArgs e)
{
lblSelectedMovie.Text = ddlMovies.SelectedItem.Text;
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Select Movie</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:DropDownList
id=”ddlMovies”
DataSourceID=”srcMovies”
DataTextField=”Title”
DataValueField=”Id”
Runat=”server” />
<asp:Button
id=”btnSelect”
Text=”Select”
OnClick=”btnSelect_Click”
Runat=”server” />
<hr />
<asp:Label
id=”lblSelectedMovie”
Runat=”server” />
<asp:SqlDataSource
id=”srcMovies”
SelectCommand=”SELECT Id, Title FROM Movies”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
Runat=”server” />
</div>
Trang 5</form>
</body>
</html>
The SelectedItem property retrieves the selected ListItem control from the DropDownList
control The value of the selected item’s Text property displays in the Label control
You can use these properties when you want to associate a List control with another
DataBound control For example, the page in Listing 10.5 contains a DropDownList control
that displays a list of movie categories and a GridView control that displays a list of
movies that match the selected category (see Figure 10.5)
FIGURE 10.5 Master/Details form with a list control
LISTING 10.5 ShowMoviesByCategory.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<style type=”text/css”>
.gridView
{
Trang 6margin-top:20px;
}
.gridView td, gridView th
{
padding:10px;
}
</style>
<title>Show Movies by Category</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:DropDownList
id=”ddlMovieCategory”
DataSourceID=”srcMovieCategories”
DataTextField=”Name”
DataValueField=”Id”
Runat=”server” />
<asp:Button
id=”btnSelect”
Text=”Select”
Runat=”server” />
<asp:GridView
id=”grdMovies”
DataSourceID=”srcMovies”
CssClass=”gridView”
Runat=”server” />
<asp:SqlDataSource
id=”srcMovieCategories”
SelectCommand=”SELECT Id, Name FROM MovieCategories”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
Runat=”server” />
<asp:SqlDataSource
id=”srcMovies”
SelectCommand=”SELECT Title,Director FROM Movies
WHERE CategoryId=@Id”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
Runat=”server”>
<SelectParameters>
Trang 7<asp:ControlParameter
Name=”Id”
ControlID=”ddlMovieCategory”
PropertyName=”SelectedValue” />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
The DropDownList control is bound to the srcMovieCategories SqlDataSource control,
and the GridView control is bound to the srcMovies SqlDataSource control The
srcMovies SqlDataSource control includes a ControlParameter, which represents the
SelectedValue property of the DropDownList control When you select a movie category
from the DropDownList control, the selected value changes, and the GridView control
displays a list of matching movies
Appending Data Items
You can mix the list items that you declare in a List control and the list items that are
added to the control when it is bound to a data source This is useful when you want to
display a default selection
For example, imagine that you create a form in which you want to require a user to pick
an item from a List control In this situation, you should add a default item to the List
control so that you can detect whether a user has actually picked an item
You can mix declarative list items with databound list items by assigning the value True to
the AppendDataBoundItems property The page in Listing 10.6 illustrates how you can add
a default list item to a List control (see Figure 10.6)
Trang 8LISTING 10.6 AppendListItems.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Append List Items</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:DropDownList
id=”ddlMovies”
DataSourceID=”srcMovies”
DataTextField=”Title”
DataValueField=”Id”
AppendDataBoundItems=”True”
Runat=”server”>
<asp:ListItem
Text=”Select a Movie”
Value=”” />
FIGURE 10.6 Displaying a default list item
Trang 9</asp:DropDownList>
<asp:RequiredFieldValidator
id=”valMovies”
Text=”(Required)”
ControlToValidate=”ddlMovies”
Runat=”server” />
<br /><br />
<asp:Button
id=”btnSubmit”
Text=”Submit Form”
Runat=”server” />
<asp:SqlDataSource
id=”srcMovies”
SelectCommand=”SELECT Id, Title FROM Movies”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
Runat=”server” />
</div>
</form>
</body>
</html>
The page in Listing 10.6 includes both a DropDownList control and a
RequiredFieldValidator control The DropDownList control includes a list item that
displays the text Select a Movie The Value property of this list item is set to the empty
string If you attempt to submit the form without selecting a list item other than the
default list item, the RequiredFieldValidator displays an error message
The DropDownList control includes an AppendDataBoundItems property that is set to the
value True If you neglect to set this property, the databound list items overwrite any
declarative list items
Enabling Automatic PostBacks
All the List controls, except for the BulletedList control, support a property named the
AutoPostBack property When this property is assigned the value True, the form
contain-ing the List control is automatically posted back to the server whenever a new selection is
made
Trang 10For example, the page in Listing 10.7 contains a DropDownList control that has its
AutoPostBack property enabled When you select a new item from the DropDownList
control, the page is automatically posted back to the server and the Label control displays
the selected item
LISTING 10.7 AutoPostBackListControl.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
protected void ddlMovies_SelectedIndexChanged(object sender, EventArgs e)
{
lblSelectedMovie.Text = ddlMovies.SelectedItem.Text;
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>AutoPostBack List Control</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:DropDownList
id=”ddlMovies”
DataSourceID=”srcMovies”
DataTextField=”Title”
DataValueField=”Id”
AutoPostBack=”true”
OnSelectedIndexChanged=”ddlMovies_SelectedIndexChanged”
Runat=”server” />
<br /><br />
<asp:Label
id=”lblSelectedMovie”
Runat=”server” />
<asp:SqlDataSource
id=”srcMovies”