13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

118<strong>ASP</strong>.<strong>NET</strong> <strong>3.5</strong>: A Beginner’s <strong>Guide</strong>and within that is a single container. You can add as many option elementsas you want. Typically the top option is the name of the menu, and then each selectableoption is a category or case within that set. For example, the following shows how aSelect form is set up in <strong>ASP</strong>.<strong>NET</strong>:CountryAfghanistanAlbaniaAlgeriaAn important attribute to include in the HTML Select object is runat="server".If you forget to add that attribute, it will not be recognized by the C# script. Also, youhave to decide whether the Select list is going to allow multiple choices at once or ismutually exclusive. If you want the user to select multiple options, then you must add theattribute and valuemultiple="multiple"within the tag. Of course, you may want to add some of the other propertiesas well, and further on in the book you will be looking at some properties useful whenworking with specific kinds of data.C# Reading and Writing HTML Select Forms The easiest way to think of an HTMLSelect form is as an array. The name of the array is the value assigned to the id attribute.In the example, the default name Select1 is the id value. Each option in the form canbe treated as an array element. You may reference the options as Items in an array. Forexample, if we take the second item in the Select form, on a zero-based array, we couldget Afghanistan as the string variable in the following line:String someCountry = Select1.Items[1];//someCountry would have the value AfghanistanOf course when you are programming in C#, the only values that you want are thoseselected by the user. One of the properties, Selected, is a Boolean. So, if the userselects the fourth element, for example, the value of that item would be true. In thepreceding listing, the value of the fourth item would be Algeria. The good thing aboutarrays is that you can use a loop to iterate through them. In that way if you have a longlist, it’s easy to check each one with a minimum of code. In the case of the Select form,you will be looking to see if the option is selected. If it is selected, then your code can passit on to wherever you want it to go. The following shows a typical loop for inspecting the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!