In many web applications we want to load a control based on what is selected in another control. For example, loading a ListBox control or a DropDownList control based on what is selected in another ListBox or DropDownList control. The situation is not limited to only these controls but could also use RadioButtonList, CheckBoxList controls, etc.
In this example, I have created a page that populates ListBox control with States value based on what Country is selected in another ListBox. I have added 2 ListBox controls – for State and Country, a Button and a Label control to the page. From code-behind page, I have created a DataTable that holds the country and corresponding state values. When an item is selected from Country ListBox, the State ListBox is populated. The AutoPostBack property for Country ListBox is set to true and SelectedIndexChanged event is implemented.
When the Button is clicked, the Label control’s Text property is populated to display the state and country that has been selected from the ListBox controls. The code for the aspx page and code-behind is below.