ASP.NET Tutorial/ASP.net Controls/ListItem — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:00, 26 мая 2010
Содержание
- 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 Choose multiple colors (use ctrl or shift):
Add asp:ListItem to asp:DropDownList
<source lang="csharp">
<%@ 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">
<asp:DropDownList id="DropDownList1" Runat="server"> <asp:ListItem Text="Oranges" /> <asp:ListItem Text="Apples" /> </asp:DropDownList> <asp:Button id="Button1" Text="Submit" Runat="server" />
</form>
</body> </html></source>
All items and selected item in a selectable control
<source lang="csharp">
<%@ 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">
<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>
<asp:DropDownList runat="server" ID="DropdownList1"> <asp:ListItem Selected="true">Option 1</asp:ListItem> <asp:ListItem>Option 2</asp:ListItem> </asp:DropDownList>
<asp:CheckBoxList runat="server" ID="CheckboxList1" RepeatColumns="3" > <asp:ListItem Selected="true">Option 1</asp:ListItem> <asp:ListItem>Option 2</asp:ListItem> </asp:CheckBoxList>
<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"/>
</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("Selected items for Listbox1:
"); foreach (ListItem li in Listbox1.Items) { if (li.Selected) Response.Write("- " + li.Text + "
"); } Response.Write("Selected item for DropdownList1:
"); Response.Write("- " + DropdownList1.SelectedItem.Text + "
"); Response.Write("Selected items for CheckboxList1:
"); foreach (ListItem li in CheckboxList1.Items) { if (li.Selected) Response.Write("- " + li.Text + "
"); } Response.Write("Selected item for RadiobuttonList1:
"); Response.Write("- " + RadiobuttonList1.SelectedItem.Text + "
"); }
}</source>
A simple BulletedList control
<source lang="csharp">
<%@ 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></source>
Change style for ListItem
<source lang="csharp">
<%@ 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">
<asp:Label ID="myLab" runat="server" />
<asp:DropDownList ID="myDrop" runat="server"> <asp:ListItem>Red</asp:ListItem> <asp:ListItem>Blue</asp:ListItem> <asp:ListItem>Green</asp:ListItem> </asp:DropDownList>
</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; }
}</source>
Declaring List Items
<source lang="csharp">
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">
<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>
</form>
</body> </html></source>
Disabling certain ListItems from a collection (C#)
<source lang="csharp">
<%@ Page Language="C#" %> <script runat="server">
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e) { Response.Write("You selected item number " + DropDownList1.SelectedValue + "
"); 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></source>
Disabling certain ListItems from a collection (VB)
<source lang="csharp">
<%@ 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 & "
") 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></source>
Set style for label
<source lang="csharp">
<%@ 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">
Choose multiple colors (use ctrl or shift):
<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" />
</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:
"; foreach (ListItem li in myList.Items) { if (li.Selected) { labResult.Text += "" + li.Text; labResult.Text += "
"; } } } }
}</source>
Using the LinkButton value for the DisplayMode attribute (C#)
<source lang="csharp">
<%@ 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 + "
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></source>
Using the LinkButton value for the DisplayMode attribute (VB)
<source lang="csharp">
<%@ 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 & _ "
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></source>