ASP.NET Tutorial/ASP.net Controls/ListItem
Содержание
- 1 Add asp:ListItem to asp:DropDownList
- 2 All items and selected item in a selectable control
- 3 A simple BulletedList control
- 4 Change style for ListItem
- 5 Declaring List Items
- 6 Disabling certain ListItems from a collection (C#)
- 7 Disabling certain ListItems from a collection (VB)
- 8 Set style for label
- 9 Using the LinkButton value for the DisplayMode attribute (C#)
- 10 Using the LinkButton value for the DisplayMode attribute (VB)
Add asp:ListItem to asp:DropDownList
<%@ Page Language="VB" Trace="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show Control Tree</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList
id="DropDownList1"
Runat="server">
<asp:ListItem Text="Oranges" />
<asp:ListItem Text="Apples" />
</asp:DropDownList>
<asp:Button
id="Button1"
Text="Submit"
Runat="server" />
</div>
</form>
</body>
</html>
All items and selected item in a selectable control
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SelectableListControls" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox runat="server"
ID="Listbox1"
SelectionMode="Multiple"
Rows="5">
<asp:ListItem Selected="true">Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:ListBox>
<br/><br/>
<asp:DropDownList runat="server" ID="DropdownList1">
<asp:ListItem Selected="true">Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:DropDownList>
<br/><br/>
<asp:CheckBoxList runat="server"
ID="CheckboxList1"
RepeatColumns="3" >
<asp:ListItem Selected="true">Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:CheckBoxList>
<br/>
<asp:RadioButtonList runat="server"
ID="RadiobuttonList1"
RepeatDirection="Horizontal"
RepeatColumns="2">
<asp:ListItem Selected="true">Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"/>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class SelectableListControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
for (int i = 3; i <= 5; i++)
{
Listbox1.Items.Add("Option " + i.ToString());
DropdownList1.Items.Add("Option " + i.ToString());
CheckboxList1.Items.Add("Option " + i.ToString());
RadiobuttonList1.Items.Add("Option " + i.ToString());
}
}
}
protected void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("<b>Selected items for Listbox1:</b><br/>");
foreach (ListItem li in Listbox1.Items)
{
if (li.Selected) Response.Write("- " + li.Text + "<br/>");
}
Response.Write("<b>Selected item for DropdownList1:</b><br/>");
Response.Write("- " + DropdownList1.SelectedItem.Text + "<br/>");
Response.Write("<b>Selected items for CheckboxList1:</b><br/>");
foreach (ListItem li in CheckboxList1.Items)
{
if (li.Selected) Response.Write("- " + li.Text + "<br/>");
}
Response.Write("<b>Selected item for RadiobuttonList1:</b><br/>");
Response.Write("- " + RadiobuttonList1.SelectedItem.Text + "<br/>");
}
}
A simple BulletedList control
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>BulletedList Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:BulletedList ID="Bulletedlist1" Runat="server">
<asp:ListItem>United States</asp:ListItem>
<asp:ListItem>United Kingdom</asp:ListItem>
<asp:ListItem>Finland</asp:ListItem>
<asp:ListItem>Russia</asp:ListItem>
<asp:ListItem>Sweden</asp:ListItem>
<asp:ListItem>Estonia</asp:ListItem>
</asp:BulletedList>
</form>
</body>
</html>
Change style for ListItem
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ListControlAttributes" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="myLab" runat="server" /><br />
<asp:DropDownList ID="myDrop" runat="server">
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ListControlAttributes : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myLab.Text = "Red text here";
myLab.Attributes["style"] = "background-color:red;";
myDrop.Items[0].Attributes["style"] = "background-color:red;";
myDrop.Items[1].Attributes["style"] = "background-color:green;";
myDrop.Items[2].Attributes["style"] = "background-color:blue;";
myDrop.Attributes["style"] = "background-color:yellow;";
myDrop.Items[1].Enabled = false;
}
}
Declaring List Items
The ListItem control supports the following five properties:
Attributes: attributes of a list item.
Enabled: disable or enable a list item.
Selected: mark as selected.
Text: text displayed by the list item.
Value: specify a hidden value associated with the list item.
<%@ 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>Favorite Product</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblProducts"
Text="Favorite Product:"
AssociatedControlID="rblProducts"
Runat="server" />
<asp:RadioButtonList
id="rblProducts"
Runat="server">
<asp:ListItem
Text="The Remains of the Day"
Value="product1" />
<asp:ListItem
Text="Star Wars"
Value="product2" />
<asp:ListItem
Text="Pulp Fiction"
Value="product3" />
</asp:RadioButtonList>
</div>
</form>
</body>
</html>
Disabling certain ListItems from a collection (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("You selected item number " + DropDownList1.SelectedValue + "<br>");
Response.Write("You didn"t select item number " + DropDownList1.Items[1].Value);
}
</script>
<html>
<head id="Head1" runat="server">
<title>DropDownList Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="Dropdownlist1_SelectedIndexChanged">
<asp:ListItem Value="1">First Choice</asp:ListItem>
<asp:ListItem Value="2" Enabled="False">Second Choice</asp:ListItem>
<asp:ListItem Value="3">Third Choice</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>
Disabling certain ListItems from a collection (VB)
<%@ page language="VB" %>
<script runat="server">
Protected Sub Dropdownlist1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Write("You selected item number " & _
DropDownList1.SelectedValue & "<br>")
Response.Write("You didn"t select item number " & _
DropDownList1.Items(1).Value)
End Sub
</script>
<html>
<head id="Head1" runat="server">
<title>DropDownList Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="Dropdownlist1" Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="Dropdownlist1_SelectedIndexChanged">
<asp:ListItem Value="1">First Choice</asp:ListItem>
<asp:ListItem Value="2" Enabled="False">Second Choice</asp:ListItem>
<asp:ListItem Value="3">Third Choice</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>
Set style for label
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ListControlProgrammatic" %>
<!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 runat="server">
<title>Programmatic Iteration of ListControl</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<h1>Choose multiple colors (use ctrl or shift): </h1>
<asp:ListBox ID="myList" runat="server"
SelectionMode="Multiple" Rows="6">
<asp:ListItem Value="#FF0000">Red</asp:ListItem>
<asp:ListItem Value="#FF00FF">Magenta</asp:ListItem>
<asp:ListItem Value="#FFFF00">Yellow</asp:ListItem>
<asp:ListItem Value="#00FF00">Green</asp:ListItem>
<asp:ListItem Value="#00FFFF">Cyan</asp:ListItem>
<asp:ListItem Value="#0000FF">Blue</asp:ListItem>
</asp:ListBox>
<asp:Button ID="myBtn" runat="server" Text="Submit"/>
<asp:Label ID="labResult" runat="server" Width="300" Height="100" />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ListControlProgrammatic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
labResult.Text = "Colors chosen: <br/>";
foreach (ListItem li in myList.Items)
{
if (li.Selected)
{
labResult.Text += "<span style="color:";
labResult.Text += li.Value + "">" + li.Text;
labResult.Text += "</span><br/>";
}
}
}
}
}
Using the LinkButton value for the DisplayMode attribute (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void BulletedList1_Click(object sender,
System.Web.UI.WebControls.BulletedListEventArgs e)
{
Label1.Text = "The index of item you selected: " + e.Index +
"<br>The value of the item selected: " +
BulletedList1.Items[e.Index].Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>BulletedList Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:BulletedList ID="BulletedList1" Runat="server"
OnClick="BulletedList1_Click" DisplayMode="LinkButton">
<asp:ListItem>United States</asp:ListItem>
<asp:ListItem>United Kingdom</asp:ListItem>
<asp:ListItem>Finland</asp:ListItem>
</asp:BulletedList>
<asp:Label ID="Label1" Runat="server">
</asp:Label>
</form>
</body>
</html>
Using the LinkButton value for the DisplayMode attribute (VB)
<%@ Page Language="VB"%>
<script runat="server">
Protected Sub BulletedList1_Click(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.BulletedListEventArgs)
Label1.Text = "The index of item you selected: " & e.Index & _
"<br>The value of the item selected: " & _
BulletedList1.Items(e.Index).Text
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>BulletedList Server Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:BulletedList ID="BulletedList1" Runat="server"
OnClick="BulletedList1_Click" DisplayMode="LinkButton">
<asp:ListItem>United States</asp:ListItem>
<asp:ListItem>United Kingdom</asp:ListItem>
<asp:ListItem>Finland</asp:ListItem>
</asp:BulletedList>
<asp:Label ID="Label1" Runat="server">
</asp:Label>
</form>
</body>
</html>