ListItem.Text Property
Gets or sets the text displayed in a list control for the item represented by the ListItem.
Assembly: System.Web (in System.Web.dll)
<PersistenceModeAttribute(PersistenceMode.EncodedInnerDefaultProperty)> Public Property Text As String
Property Value
Type: System.StringThe text displayed in a list control for the item represented by the ListItem control. The default value is String.Empty.
Use the Text property to specify or determine the text displayed in a list control for the item represented by the ListItem.
Note |
|---|
If the Text property contains null, the get accessor returns the value of the Value property. If the Value property, in turn, contains null, String.Empty is returned. |
The value of this property, when set, can be saved automatically to a resource file by using a designer tool. For more information, see LocalizableAttribute and ASP.NET Globalization and Localization.
Note |
|---|
The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model. |
<%@ Page Language="VB" %> <!-- The following example demonstrates adding items to and removing items from ListBox controls. When an item is selected in ListBox1, a new ListBoxItem with the same value can be created and added to ListBox2, if ListBox2 does not already contain an item with that value. When the new ListBoxItem is created, it receives the Value property of the selected item as its Text property, and the Text property of the selected item as its value property. --> <!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> <title>ListItem Example</title> <script language="VB" runat="server"> Sub AddBtn_Click(Sender As Object, e As EventArgs) If ListBox1.SelectedIndex > -1 Then If ListBox2.Items.FindByValue(ListBox1.SelectedItem.Text) is Nothing Then Dim Item As ListItem = new ListItem() 'Text and Value are swapped Item.Text = ListBox1.SelectedItem.Value Item.Value = ListBox1.SelectedItem.Text ListBox2.Items.Add(Item) End If End If End Sub Sub DelBtn_Click(Sender As Object, e As EventArgs) If ListBox2.SelectedIndex > -1 Then ListBox2.Items.Remove(ListBox2.SelectedItem) End If End Sub </script> </head> <body> <h3>ListItem Example</h3> <form id="form1" runat="server"> <table> <tr><td> <asp:ListBox id="ListBox1" Width="100px" runat="server"> <asp:ListItem Value="Value 1">Item 1</asp:ListItem> <asp:ListItem Value="Value 2">Item 2</asp:ListItem> <asp:ListItem Value="Value 3">Item 3</asp:ListItem> <asp:ListItem Value="Value 4">Item 4</asp:ListItem> <asp:ListItem Value="Value 5" Selected="True">Item 5</asp:ListItem> <asp:ListItem Value="Value 6">Item 6</asp:ListItem> </asp:ListBox> </td><td> <asp:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br /> <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" /> </td><td> <asp:ListBox id="ListBox2" Width="100px" runat="server"/> </td></tr> </table> </form> </body> </html>
Available since 1.1
Value
ListControl
RadioButtonList
ListBox
DropDownList
CheckBoxList
ListItem Class
System.Web.UI.WebControls Namespace
ListBox Web Server Control Overview
RadioButton and RadioButtonList Web Server Controls Overview
CheckBox and CheckBoxList Web Server Controls Overview
BulletedList Web Server Control Overview
DropDownList Web Server Control Overview
