六、ListItem 类
ListItem 控件表示 ListBox 或 RadioButtonList 控件等数据绑定列表控件中的个别数据项。
有几种方法可以指定为列表控件中的项显示的文本。最常用的方法是将文本放在内部 HTML 内容中。内部 HTML 内容是 ListItem 控件的开始标记和结束标记之间的文本。还可以使用 Text 属性指定列表控件中为项显示的文本。
Value 属性使您得以除了在控件中显示的文本外,还可以将值与列表控件中的项关联。例如,可以为列表控件中的项显示文本(如 "Item 1"),并使用 Value 属性为该项指定值,如 "$1.99"。
可以设置内部 HTML 内容、Text 或 Value 属性的任意组合。ListItem 控件的结果 HTML 输出取决于所设置的这三种属性的组合。例如,如果所有三种属性按如下设置:
<asp:ListItem Value="Value 1" Text="Item 1">Inner 1</asp:ListItem>
内部 HTML 内容用于呈现的内部 HTML 内容,而 Value 属性用于 Value 属性。结果 HTML 呈现输出为:
<option value="Value 1">Inner 1</option>
下表列出了已设置属性 (Property) 的组合以及用于呈现的内部 HTML 内容和 Value 属性 (Attribute) 的相应属性 (Property)。左边的三列列出了已设置属性的组合。右边的两列列出了用于相应特性的属性值。
内部 HTML 内容 |
文本属性 |
值属性 |
呈现的“内部 HTML”内容 |
呈现的“值”属性 |
已设置 |
已设置 |
已设置 |
内部 HTML 内容 |
值属性 |
已设置 |
已设置 |
未设置 |
内部 HTML 内容 |
内部 HTML 内容 |
已设置 |
未设置 |
已设置 |
内部 HTML 内容 |
值属性 |
已设置 |
未设置 |
未设置 |
内部 HTML 内容 |
“内部 HTML”文本 |
未设置 |
已设置 |
已设置 |
文本属性 |
值属性 |
未设置 |
已设置 |
未设置 |
文本属性 |
文本属性 |
未设置 |
未设置 |
已设置 |
值属性 |
值属性 |
未设置 |
未设置 |
未设置 |
未设置 |
未设置 |
说明: 由于 Text 和 Value 属性都具有空字符串默认值,所以列表控件中可能有空列表项。
当显示列表控件时,任何 Selected 属性设置为 true 的 ListItem 控件在此控件中突出显示。
使用 ListItem 控件提供的 Enabled 属性可以指定是启用还是禁用 ListItem 控件。禁用的 ListItem 控件显示为灰色,指示不能选择该控件。使用此属性可禁用 RadioButtonList 控件或 CheckBoxList 控件中的 ListItem 控件。
说明: 不能使用此属性禁用 DropDownList 控件或 ListBox 控件中的 ListItem 控件。
有关 ListItem 的实例的初始属性值列表,请参见 ListItem 构造函数。
警告: 此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在应用程序中显示从客户端发送来的任何信息之前,先检查这些信息中是否含有可执行脚本、SQL 语句或其他代码。在用控件显示输入文本之前,可以使用验证控件验证用户输入。ASP.NET 提供了输入请求验证功能,用于阻止用户输入中的脚本和 HTML。
示例
下面的示例阐释如何使用 ListBox 控件中的 ListItem 控件。
说明: 下面的代码示例使用单文件代码模型;在将这些代码示例直接复制到代码隐藏文件中时,它们可能无法正常工作。必须将每个代码示例都复制到具有 .aspx 扩展名的空文本文件中
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>ListBox Example</title>
<script language="C#" runat="server">
void SubmitBtn_Click(Object Sender, EventArgs e) {
if (ListBox1.SelectedIndex > -1) {
Label1.Text="You chose: " + ListBox1.SelectedItem.Text;
Label1.Text+="<br /> with value: " + ListBox1.SelectedItem.Value;
}
}
</script>
</head>
<body>
<h3>ListBox Example</h3>
<br />
<form id="form1" runat="server">
<asp:ListBox id="ListBox1" Width="100px" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem Value="Value 4">Item 4</asp:ListItem>
<asp:ListItem Text="Item 5" Value="Value 5" Selected="True"/>
<asp:ListItem>Item 6</asp:ListItem>
</asp:ListBox>
<asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server" />
<br />
<asp:Label id="Label1" font-names="Verdana" font-size="10pt" runat="server"/>
</form>
</body>
</html>
<!-- This example demonstrates how to select multiple items from a DataList and add the
selected items to a DataGrid. The example uses a foreach loop to iterate through
the ListItem objects in the ListItemCollection of ListBox1. -->
...
<%@ Page language="c#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="C#" runat="server">
// Global Variables.
private DataView dv;
private DataTable dt = new DataTable();
private void Page_Load(object sender, System.EventArgs e)
{
// Set the number of rows displayed in the ListBox to be
// the number of items in the ListBoxCollection.
ListBox1.Rows = ListBox1.Items.Count;
// If the DataTable is already stored in the Web form's default
// HttpSessionState variable, then don't recreate the DataTable.
if (Session["data"] == null)
{
// Add columns to the DataTable.
dt.Columns.Add(new DataColumn("Item"));
dt.Columns.Add(new DataColumn("Price"));
// Store the DataTable in the Session variable so it can
// be accessed again later.
Session["data"] = dt;
// Use the table to create a DataView, because the DataGrid
// can only bind to a data source that implements IEnumerable.
dv = new DataView(dt);
// Set the DataView as the data source, and bind it to the DataGrid.
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
}
private void addButton_Click(object sender, System.EventArgs e)
{
// Add the items selected in ListBox1 to DataGrid1.
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
// Add the item to the DataGrid.
// First, get the DataTable from the Session variable.
dt = (DataTable)Session["data"];
if (dt != null)
{
// Create a new DataRow in the DataTable.
DataRow dr = dt.NewRow();
// Add the item to the new DataRow.
dr["Item"] = item.Text;
// Add the item's value to the DataRow.
dr["Price"] = item.Value;
// Add the DataRow to the DataTable.
dt.Rows.Add(dr);
// Rebind the data to DataGrid1.
dv = new DataView(dt);
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
}
}
}
</script>
<html >
<head>
<title> ListItemCollection Example </title>
</head>
<body>
<form id="form1" runat="server">
<h3> ListItemCollection Example </h3>
<table cellpadding="6" border="0">
<tr>
<td valign="top">
<asp:ListBox id="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem Value=".89">apples</asp:ListItem>
<asp:ListItem Value=".49">bananas</asp:ListItem>
<asp:ListItem Value="2.99">cherries</asp:ListItem>
<asp:ListItem Value="1.49">grapes</asp:ListItem>
<asp:ListItem Value="2.00">mangos</asp:ListItem>
<asp:ListItem Value="1.09">oranges</asp:ListItem>
</asp:ListBox>
</td>
<td valign="top">
<asp:Button id="addButton" runat="server" Text="Add -->"
Width="100px" OnClick="addButton_Click"></asp:Button>
</td>
<td valign="top">
<asp:DataGrid Runat="server" ID="DataGrid1" CellPadding="4">
</asp:DataGrid>
</td>
</tr>
</table>
</form>
</body>
</html>