BrowserHawk IQ now available! Click here for the full scoop!
Search:
How can I use GetSelectList from code behind pages?
Created: 4/26/2003    Updated: 7/15/2003
Q   I want to use GetSelectList to automatically populate a drop down box on my web form with all the country names, with the user's detected country highlighted by default. How can I use this method with my code behind pages from VS.NET?

A   Starting with CountryHawk 2.01 there is now a new method signature for GetSelectList in the native .NET version which takes a ListItemCollection class as a parameter.

GetSelectList then creates ListItems for each country that is in the drop down list, and adds each ListItem to the ListItemCollection you passed in.

To use the new GetSelectList from code behind you simply do this:

1) Create your web form with all form fields as you normally would

2) When you get to the field that you want to serve as the country selection list box, add that field as a DropDownList control (most likely the best suited control for this purpose) or any other control that stores a list of names and values as a ListItemCollection for that matter (such as a ListBox control).

Take note of the name you assign to this control. For purposes of this discussion let's say you call it "CountrySelectionBox".

3) In the code behind page for the form, add the following line to the list of other "using" declarations:

        using cyScape.CountryHawk;

4) In the code behind page for the form, add the following two lines to the Page_Load event:

	CountryObj chObj = CountryObj.GetCountry();
	chObj.GetSelectList(false, null, "US", null, CountrySelectionBox.Items);

The most important thing to note is the CountrySelectionBox.Items. This passes the web form's ListItemCollection to CountryHawk so it can populate the results for you. If you use a different name for your control, be sure to change that to reflect your contro's name (i.e. YourControlName.Items).

In the example above, the United States will be the default selected country just to demonstrate how you can control that if you wish. Typically you would want to have the user's detected country be the default selected - to do this just pass in null for the parameter where "US" is shown above. See the documentation for more information on what the other parameters to this method control.

5) To retrieve the result of the selected country in the form's post processing, just use the same technique that you would normally use for retrieving a form field value.

Note that your drop down list control is a regular web form control. CountryHawk just takes care of populating the list for you and highlighting the default country based on your preferences.

TIP: Starting with CountryHawk 2.01 there is a new sample installed called CHCodeBehind1. This sample demonstrates how to use CountryHawk from code behing pages in general, as well as how to use the GetSelectList method from code behind pages.

    FAQ Home  |  Search FAQ  |  Show New Items  |  Ask a FAQ  |  Notify Me  |  Most Popular     
 
 
Copyright © 1994-2020 cyScape, Inc. All Rights Reserved. ()
ASP, ASP.NET, ColdFusion MX, CFMX, JSP, PHP