ASP.NET Syntax ASP.NET Syntax for Web Controls
Creates a single-selection or multi-selection list box.
Declarative Syntax
<asp:ListBox
AccessKey = "string"
AppendDataBoundItems = "True | False"
AutoPostBack = "True | False"
BackColor = "color name | #dddddd"
BorderColor = "color name | #dddddd"
BorderStyle = "NotSet | None | Dotted | Dashed | Solid | Double | Groove |
Ridge | Inset | Outset"
BorderWidth = size
CausesValidation = "True | False"
CssClass = "string"
DataMember = "string"
DataSource = "string"
DataSourceID = "string"
DataTextField = "string"
DataTextFormatString = "string"
DataValueField = "string"
Enabled = "True | False"
EnableTheming = "True | False"
EnableViewState = "True | False"
Font-Bold = "True | False"
Font-Italic = "True | False"
Font-Names = "string"
Font-Overline = "True | False"
Font-Size = "string | Smaller | Larger | XX-Small | X-Small | Small |
Medium | Large | X-Large | XX-Large"
Font-Strikeout = "True | False"
Font-Underline = "True | False"
ForeColor = "color name | #dddddd"
Height = size
ID = "string"
OnDataBinding = "DataBinding event handler"
OnDataBound = "DataBound event handler"
OnDisposed = "Disposed event handler"
OnInit = "Init event handler"
OnLoad = "Load event handler"
OnPreRender = "PreRender event handler"
OnSelectedIndexChanged = "SelectedIndexChanged event handler"
OnTextChanged = "TextChanged event handler"
OnUnload = "Unload event handler"
Rows = integer
runat = "server"
SelectedIndex = integer
SelectedValue = "string"
SelectionMode = "Single | Multiple"
SkinID = "string"
Style = "string"
TabIndex = integer
ToolTip = "string"
ValidationGroup = "string"
Visible = "True | False"
Width = size
>
<asp:ListItem
Enabled = "True | False"
Selected = "True | False"
Text = "string"
Value = "string"
/>
</asp:ListBox>
For information on the individual members of this class, see ListBox in the class library.
The ListBox control renders a list control that allows users to select single or multiple items. Use the Rows property to specify the height of the control. To enable multiple item selection, set the ListBox.SelectionMode Property property to ListSelectionMode.Multiple.
The below code snippet demonstrates how to use a simple ListBox control.
Within the <head
> of a Web Forms page:
<script language="C#" runat="server">
void SubmitBtn_Click ( Object sender, EventArgs e ) {
if ( ListBox1.SelectedIndex > -1 ) {
msgLabel.Text = "The first item you chose: " + ListBox1.SelectedItem.Text;
}
}
</script>
<script language="VB" runat="server">
Sub SubmitBtn_Click ( sender As Object, e As EventArgs )
If ListBox1.SelectedIndex > - 1 Then
msgLabel.Text = "You chose: " & ListBox1.SelectedItem.Text
End If
End Sub
</script> |
|
C# |
VB |
Within the <body
> of a Web Forms page:
<form runat=server>
<asp:ListBox id="ListBox1" Rows = "6"
SelectionMode = "Single" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:ListBox>
<asp:button id="Button1" Text = "Submit"
onClick = "SubmitBtn_Click" runat="server" />
<p>
<asp:Label id="msgLabel" runat="server" />
</form>
ListBox Class ListBox Web Server Control