Learn how to handle the Selected Index Changed Event.


More DIY videos at 5min.com

Video Transcription

First of all, I want to handle the other two events on the page. There is the selected index change event on the first box. So let us handle the selected index change event there. Okay, so when this event gets triggered, it means the user has chosen a different region. So let us get the region the user has now selected. Okay, so we have got the selected value from the region list and that while you are it, let us just display that region on the business card, BusinessCardLocationLabel.Text = Region. Okay, so that will display the users region on the business card. And let us populate the country list box with the countries for that region. Now things first, we better get rid of the items that are concurrently in the country list. So to do that, we say country list. We get the collection of items and we clear that collection of items. So the country list is now empty. Let us add first of all, an item called please select. Like so and now, let us add all the countries for the user’s selected region. So for example the region is Asia Pacific, we need to add all the countries for the Asia Pacific region. So to do that, the code we need is like so. Okay, so we have specified the items for the country list and the add range method enable us to add a whole collection of list items in one go. So, we need to get our region country data. Remember, that is the name of our instance variable and we need index into that collection specifying the region that the user has actually selected. And that will return all the countries for that region and it will add those countries to the country list. Let us run the application and see if it works so far. All of the application is already running. Let us click cancel there and I am going to close it, and I am going to run it again just to avoid any possible confusion. So, I am running out of names to enter here. Let me enter Gerald Price and telephone number-12345. Now, Gerald Price is in the region Kenya. Oh great! Look at that! The country list has—not there. But the reason for that is because we have not clicked the update button. It is the same problem we had to text boxes, remember? The text change to them did occur, but it only occurred when we check the update button. It did not occur automatically when we change the text. And likewise here, I have changed the region but we have not posted back to the server yet. So the server has not have an opportunity to recognize the change in selected item and to trigger the event. So if I click the update button, now, it has populated the country list.