Version

Binding WebDropDown to a Web Service

Before You Begin

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.

What You Will Accomplish

You will learn how to bind WebDropDown to a string array returned by a Web Service.

Note
Note:

Building and designing a web service is beyond the scope of this topic. This topic demonstrates a synchronous way of retrieving data. For more advanced and data intensive applications, an asynchronous approach should be taken. For more information on Web Services, please visit the link: MSDN ®.

Follow these Steps

  1. From the Visual Studio™ Toolbox, drag and drop a ScriptManager component and a WebDropDown control.

  2. 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;
      }
  1. 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();
  1. Save and run your application. Your WebDropDown now looks similar to the following image:

images\WebDropDown Binding WebDropDown to a Web Service 01.png