Learn how to perform initialization of list box programmatic.
Tags:asp list box control,asp.net,microsoft visual c#,microsoft windows,total training,visual studio 2005,web development
Grab video code:
Transcript
Now, I am actually going to delete those items there. Let’s delete that. So, we will just have the list box load any items. And what we will see now is how to perform programmatic initialization. Imagine for example the scenario where the countries that we want to add to all this box is not known at development time, maybe it is going to change every time you run the application. Maybe we need to get the list of countries from the data base or maybe we need to call a web service to get the data from some dynamic place. So, rather than having fixed initialization, we file, default.aspx.cs. And if I just double click on that. That is the whole purpose for the Page Load Method. The Page Load Method gives us an opportunity and the place where we can perform programmatic initialization of the controls that make up our web page. Okay. So, for example, I can make code like this. CountryList.items.add and I can add an item to a list box programmatically. So, let us add Australia. Okay. And I am just going to copy that line of text to the clipboard and I am just going to paste it back then again a few times, paste, paste and paste. So, I am going to add various items to the list box, so let me see. Let us add Canada and let us also add France and we will also add Germany, there we go. So, just some countries selected at random. Okay. Well, let us try running the application and see what happens so, Control-F5 to run the application. Okay. Well, we’ve certainly have some work to do on the layout but then it is looking good. It has a label that displays some static initialization and there is the text box where I am going to enter my name so, Andy. And I can choose some items from here. Okay. That is very good. Just close on that. Now, that is fine. The only problem is that every time we post back to the web server, this code is going to be re-executed which means for example, the next time you post it back, Australia, Canada, France and Germany will be added again and then the next time you post back, they will be added again. It will be an accumulated effect which will obviously not what we want to achieve. Really, we only want to perform this initialization the very first time the pages loaded and not on a subsequent post back. And now, because it is such a common requirement, the page class has a static or shared property called IsPostBack. And that helps you to differentiate between whether this was the first time the pages has been requested or whether it is a subsequent post back. So, I need to add a statement like this. If it is not post back then I will just indent these statements and close off with the close coded bracket. So, a page load method will be invoke every time the page is requested but this initialization code will only be executed if the value for this time it will not be executed on each subsequent post back.
Comments