Controls You Can Use on Web Forms ASP.NET Standard Controls ListBox Control
The information in this topic applies to all list Web server controls: ListBox, DropDownList, CheckBoxList, and RadioButtonList.
When the user makes a selection in a list Web server control, the control raises an event that you can respond to directly.
In certain cases, you may not want or need to respond directly to the selection event at all. In this case, you can test which item is selected after the form has been posted to the server by a control such as a Button control.
For details about determining which item is selected, see Determining the Selection in a List Control.
The following examples show how you can respond to a selection in a list control when the SelectedIndexChanged event occurs.
- Define a method to handle the control’s SelectedIndexChanged event.
private void myListHandler ( object src, EventArgs e ) {
// ... do something amusing here ...
}
Private Sub myListHandler ( ByVal src As Object, ByVal e As EventArgs )
' ... do something amusing here ...
End Sub |
|
C# |
VB |
- Assign the handler to the onSelectedIndexChanged event attribute of the list control.
<asp:dropdownlist onSelectedIndexChanged="myListHandler" runat="server" />
By default, the SelectedIndexChanged event does not immediately cause the Web Forms page to be posted to the server. Instead, the event is raised in server code the next time the form is posted ( as when a Button Web server control is clicked ). To have the SelectedIndexChanged event cause an immediate posting, set the control’s AutoPostBack property to true.
Note, however, that because the event is raised as soon as the user makes a selection in the list control, it is not practical to set AutoPostBack to true in multi-select list controls ( CheckBoxList and multi-select ListBox ), because users will not be able to make another selection.
Web Forms Events and Handlers