Click here to Skip to main content
13,145,774 members (61,553 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.7K views
4 bookmarked
Posted 20 Jun 2015

How to bind a HTML Drop Down List with Web Services in ASP.NET using C#

, 20 Jun 2015
Rate this:
Please Sign up or sign in to vote.
In this article I will show you how to bind a Drop Down List in ASP.NET site with a web services.

Introduction

In this article I will show you how to bind a Drop Down List in ASP.NET site with a web services. Before starting we need to know what is a Web Services is. Then we will know how to bind the Drop Down List with the help of that Web Service.

Web Services

In web services we use WebMethod to call with parameter and to do operations. A web Services is inherited by class System.Web.Services.WebService. To check my Web Service article click here.

Starting With a Web Service

To start process with a Web Service create a new project, add a new Web Form. To add a new Web Service go to Add New Item. And then add a new Web Service (.asmx). Name it as you want.

Now in the Web Form add a new HTML  Drop Down List. We will use the Web Service to bind the HTML Drop Down List.

<select id="ddlFrom">
</select>

Now we will check the following jQuery JSON code.

$(document).ready(function () {
            load_ddlFrom();
        });

function load_ddlFrom() {
  $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "services/Bind.asmx/LoadddlForm",
                data: "{}",
                dataType: "json",
                success: function (Result) {
                    Result = Result.d;
                    $.each(Result, function (key, value) {
                        $("#ddlFrom").append($("<option></option>").val
                        (value.Id).html(value.Stopage));
                    });
                },
                error: function (Result) {
                    alert("Error");
                }
            });
        }

Lets check the url carefully services/Bind.asmx/LoadddlForm services is the folder name where all the services are stored, I didn't use any thing before it because it is locketed in the root folder. Then Bind.asmx is the name of the web services that you are accessing. And the last one LoadForm. It is the method name that you are invoking.  So lets check what is written in the LoadForm method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace Demo.services
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Bind : System.Web.Services.WebService
{
public class CountryInfo
{
public int Id { get; set; }
public string Stopage { get; set; }
}
public List CountryInformation { get; set; }

[WebMethod]
public List LoadddlForm()
{
CountryInfo ci = new CountryInfo();
List CountryInformation = new List();
DataSet ds;
using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["booking"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("select Id,Stopage from tblStopageMeta where isdelete=0", con))
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{

ds = new DataSet();
da.Fill(ds);
}
}
}
try
{
if (ds != null)
{
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
CountryInformation.Add(new CountryInfo()
{
Id = Convert.ToInt32(dr["Id"]),
Stopage = dr["Stopage"].ToString()
});
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return CountryInformation;
}
}
}

Now run your project and check whether it is binding your HTML Drop Down List or not. But before that one create your database and database connection in Web.Config.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arkadeep De
Software Developer Cognizant
India India
I am a Software developer having an experience of 4 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 21 Jun 2015
Article Copyright 2015 by Arkadeep De
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid