Public Function GetDaysOfTheWeek() As String() Dim weekdays As String() = New String() {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", _ "Saturday"} Return weekdays End Function
WebDropDown™ supports binding to a Web Service using its DataSource property.In this walkthrough, a simple Web Service is created with a method which returns the days of the week as a string array. This string array is bound to WebDropDown so that the days of the week are displayed in the drop-down container.
You will learn how to bind WebDropDown to a string array returned by a Web Service.
From the Visual Studio™ Toolbox, drag and drop a ScriptManager component and a WebDropDown control.
Create a Web Service as shown in the following steps:
Right click on the project and click Add New Item…This will open the Add New Item dialog.
Select Web Service from the templates and change the name to myWebService.asmx. Click Add to close the dialog.
Now add a WebMethod which returns the days of the week to the Web Service as shown in the following code:
In Visual Basic:
Public Function GetDaysOfTheWeek() As String() Dim weekdays As String() = New String() {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", _ "Saturday"} Return weekdays End Function
In C#:
public string[] GetDaysOfTheWeek() { string[] weekdays = new string[]{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; return weekdays; }
Set the WebDropDown control’s DataSource property to the WebService’s GetDaysOfTheWeek WebMethod as shown in the following code:
In Visual Basic:
Dim service As New myWebService() WebDropDown1.DataSource = service.GetDaysOfTheWeek()
In C#:
myWebService service = new myWebService(); WebDropDown1.DataSource = service.GetDaysOfTheWeek();
Save and run your application. Your WebDropDown now looks similar to the following image: