Export (0) Print
Expand All

ListItem.Value Property

Gets or sets the value associated with the ListItem.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

[LocalizableAttribute(true)] 
public string Value { get; set; }
/** @property */
public String get_Value ()

/** @property */
public void set_Value (String value)

public function get Value () : String

public function set Value (value : String)

Property Value

The value associated with the ListItem. The default is String.Empty.

Use the Value property to specify or determine the value associated with the ListItem.

NoteNote

If the Value property contains a null reference (Nothing in Visual Basic), the get accessor returns the value of the Text property. If the Text property, in turn, contains a null reference (Nothing in Visual Basic), 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.

NoteNote

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 Page Code Model.

<!-- 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. -->

<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
 
     <script runat="server">
 
         void AddBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox1.SelectedIndex > -1) 
             {
             	 if (ListBox2.Items.FindByValue(ListBox1.SelectedItem.Text) == null) 
                 {
             	     ListItem Item = new ListItem();
             	     // Text and Value are swapped.
                     Item.Text = ListBox1.SelectedItem.Value;
                     Item.Value = ListBox1.SelectedItem.Text;
                     ListBox2.Items.Add(Item);
             	 }
             }
         }
 
         void DelBtn_Click(Object Sender, EventArgs e) 
         {
             if (ListBox2.SelectedIndex > -1) 
             {
                 ListBox2.Items.Remove(ListBox2.SelectedItem);
             }
         }
 
     </script>
 
 </head>
 <body>
 
     <h3>ListItem Example</h3>
     <p>
     <form 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>
          

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft