ListItem Constructor (String, String)

ListItem Constructor (String, String)

Initializes a new instance of the ListItem class with the specified text and value data.

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

public ListItem(
	string text,
	string value


Type: System.String

The text to display in the list control for the item represented by the ListItem.

Type: System.String

The value associated with the ListItem.

Use this constructor to create and initialize a new instance of the ListItem class using the specified text and value.

The following table shows initial property values for an instance of ListItem.


Initial Value


The value of the text parameter.


The value of the value parameter.




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.

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

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
    <title>ListItem Example</title>
<script runat="server">

         void AddBtn_Click(Object Sender, EventArgs e) 
             if (ListBox1.SelectedIndex > -1) 
                  if (ListBox2.Items.FindByText(ListBox1.SelectedItem.Text) == null) 
                     ListItem Item = new ListItem(ListBox1.SelectedItem.Text, 

         void DelBtn_Click(Object Sender, EventArgs e) 
             if (ListBox2.SelectedIndex > -1) 



     <h3>ListItem Example</h3>
     <form id="form1" runat="server">
         <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:button Text="--->" OnClick="AddBtn_Click" runat="server" /><br />
         <asp:button Text="<---" OnClick="DelBtn_Click" runat="server" />
         <asp:ListBox id="ListBox2" Width="100px" runat="server"/>


.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
© 2015 Microsoft