HtmlHelper.ListBox Method (String, String, IEnumerable<SelectListItem>, Object, Int32, Boolean, IDictionary<String, Object>)

Returns an HTML list box control that has the specified name, size, custom attributes defined by an attribute dictionary, items, default item, and selections, and that specifies whether multiple selections are enabled.

Namespace:  System.Web.WebPages.Html
Assembly:  System.Web.WebPages (in System.Web.WebPages.dll)


Public Function ListBox ( _
    name As String, _
    defaultOption As String, _
    selectList As IEnumerable(Of SelectListItem), _
    selectedValues As Object, _
    size As Integer, _
    allowMultiple As Boolean, _
    htmlAttributes As IDictionary(Of String, Object) _
) As IHtmlString
Dim instance As HtmlHelper 
Dim name As String 
Dim defaultOption As String 
Dim selectList As IEnumerable(Of SelectListItem)
Dim selectedValues As Object 
Dim size As Integer 
Dim allowMultiple As Boolean 
Dim htmlAttributes As IDictionary(Of String, Object)
Dim returnValue As IHtmlString 

returnValue = instance.ListBox(name, defaultOption, _
    selectList, selectedValues, size, _
    allowMultiple, htmlAttributes)
public IHtmlString ListBox(
    string name,
    string defaultOption,
    IEnumerable<SelectListItem> selectList,
    Object selectedValues,
    int size,
    bool allowMultiple,
    IDictionary<string, Object> htmlAttributes
IHtmlString^ ListBox(
    String^ name, 
    String^ defaultOption, 
    IEnumerable<SelectListItem^>^ selectList, 
    Object^ selectedValues, 
    int size, 
    bool allowMultiple, 
    IDictionary<String^, Object^>^ htmlAttributes
member ListBox : 
        name:string * 
        defaultOption:string * 
        selectList:IEnumerable<SelectListItem> * 
        selectedValues:Object * 
        size:int * 
        allowMultiple:bool * 
        htmlAttributes:IDictionary<string, Object> -> IHtmlString
public function ListBox(
    name : String, 
    defaultOption : String, 
    selectList : IEnumerable<SelectListItem>, 
    selectedValues : Object, 
    size : int, 
    allowMultiple : boolean, 
    htmlAttributes : IDictionary<String, Object>
) : IHtmlString


  • name
    Type: System.String
    The value to assign to the name attribute of the HTML select element.
  • defaultOption
    Type: System.String
    The text to display for the default option in the list.
  • selectedValues
    Type: System.Object
    An object that specifies the items in the list that are selected by default. The selections are retrieved through reflection by examining the properties of the object.
  • size
    Type: System.Int32
    The value to assign to the size attribute of the element.
  • allowMultiple
    Type: System.Boolean
    true to indicate that multiple selections are enabled; otherwise, false.

Return Value

Type: System.Web.IHtmlString
The HTML markup that represents the list box control.


Exception Condition

name is null reference (Nothing in Visual Basic) or empty.


The returned markup consists of an HTML select element in the following form:

<select name="name" size="size" multiple="multiple" ...htmlAttributes...>
  <option value="selectList[0].Value" selected="selected">selectList[0].Text</option>
  <option value="selectList[1].Value">selectList[1].Text</option>

allowMultiple is true.

<select name="name" size="size" ...htmlAttributes...>
  <option value="selectList[0].Value" selected="selected">selectList[0].Text</option>
  <option value="selectList[1].Value">selectList[1].Text</option>

allowMultiple is false.

A list item is selected when either its value or its displayed text (if the item has no value) matches one of the properties of selectedValues, which are examined through reflection. When an item is selected, the selected attribute of the associated HTML option element is set to selected.

The size attribute specifies the number of list items that are visible in the list box. If size is greater than 1 and more items are available than can be displayed, the list box displays scroll bars.

See Also


HtmlHelper Class

ListBox Overload

System.Web.WebPages.Html Namespace