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

Returns an HTML list box control that has the specified name, items, default item, and custom attributes defined by an attribute object, and selections.

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

Syntax

'Declaration
Public Function ListBox ( _
    name As String, _
    defaultOption As String, _
    selectList As IEnumerable(Of SelectListItem), _
    selectedValues As Object, _
    htmlAttributes As Object _
) As IHtmlString
'Usage
Dim instance As HtmlHelper 
Dim name As String 
Dim defaultOption As String 
Dim selectList As IEnumerable(Of SelectListItem)
Dim selectedValues As Object 
Dim htmlAttributes As Object 
Dim returnValue As IHtmlString 

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

Parameters

  • 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.
  • htmlAttributes
    Type: System.Object
    An object that contains custom attributes for the element. The attribute names and values are retrieved through reflection by examining the properties of the object.

Return Value

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

Exceptions

Exception Condition
ArgumentException

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

Remarks

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

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

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.

See Also

Reference

HtmlHelper Class

ListBox Overload

System.Web.WebPages.Html Namespace