How to populate dropdown on client side using Web service and Jquery ?



Hi
If we populate the dropdown on client side using web service and jquery, it will give very good performance as compare to server side method.

These are the few steps to do this task
It is very much similar to this post. Here we have to do the few changes in web service
1. Add this namespace in web service
using System.Web.Script.Services;
2. Add this code below the web method
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]

Complete code is like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Test;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Services;

///

/// Summary description for WebService
///

[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService {

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public List FindAllCountries()
{
List locations = new List();
string connectionString =
“Data Source=.;Initial Catalog=Test;Integrated Security=True”;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = “SelectAllCountries”;
command.CommandType = CommandType.StoredProcedure;
connection.Open();
using (SqlDataReader dataReader = command.ExecuteReader())
{
Location location;
if (dataReader != null)
while (dataReader.Read())
{
location = new Location();
location.CountryID = (int)dataReader[“CountryID”];
location.CountryName = Convert.ToString(dataReader[“CountryName”]);
locations.Add(location);
}
}
}
}
return locations;
}

}

In default page we have to call the web service using Jquery like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default_Jquery1.aspx.cs" Inherits="WebService_Test_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script&gt;

<script language="javascript" type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "GET",
url: "../WebService.asmx/FindAllCountries",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var ddl1 = response.d;
for (var i = 0; i < ddl1.length; i++) {
var selectOption = $(document.createElement(‘option’));
$(‘#ddl1’).append(selectOption.val(ddl1[i].CountryID).html(ddl1[i].CountryName)
);
}
},
failure: function(errMsg) {
$(‘#errMessage’).text(errMsg);
}
});
});
</script>

</head>
<body>
<form id="form1" runat="server">
<div>

<select id="ddl1">
<option>Select</option>
</select>
<div id="errMessage"></div>

</div>
</form>
</body>
</html>

Advertisements

16 Responses to “How to populate dropdown on client side using Web service and Jquery ?”

  1. Gaddu Says:

    grt

  2. debt consolidation loan for non home owner Says:

    I don’t even know the way I ended up right here, but I assumed this post was good. I don’t understand
    who you are but definitely you are going to a well-known blogger for
    those who aren’t already. Cheers!

    • Chandra Dev Says:

      Thank for your kind words and motivating me to do good work.

  3. Ada Says:

    It’s tгuly a greeat and helpful piece of info.
    I’m satisfied tҺat you just shared thіs helpful
    infoгmation wіtɦ սs. Pleaase keep uѕ up to date like this.
    Thanks forr sharing.

  4. technique based sales Says:

    I just liқe thhe helpfjl іnformation you provide oon ʏоur
    articles. ӏ’ll bookmark your blopg ɑnd test oncе moгe hегe
    frequently. I am գuite ceгtain I’ll bе informed
    many new stuff prooper herе! Bеst оf luck for the next!

  5. Alberton Who Is Responsible For Blocked Drains Says:

    Whoa! This blog looks just like my old one!
    It’s on a completely different topic but it has pretty much the same layout
    and design. Excellent choice of colors!

  6. Venetta Says:

    Thesse aare in fsct fantastic iideas in abot blogging.
    Youu hawve ttouched some gkod thins here.
    Any wayy keep upp wrinting.

    • Chandra Dev Says:

      Thank you.

  7. http://www.couptow.com/gbook/go.php?url=http://lisa-jerome.write2me.nl Says:

    Hi there, just beсame alert tо yߋur blog through
    Google, аnd found tat it is rally informative. І am gonna
    watch oսt fߋr brussels. Ι woll ɑppreciate if yօu continhe tɦis in future.
    А llot of people ԝill bee benefited from youг
    writing. Cheers!

  8. Genital Herpes Cures Says:

    Howdy! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in trading links or maybe guest writing a
    blog article or vice-versa? My blog covers a lot of the
    same subjects as yours and I believe we could greatly benefit from each other.
    If you might be interested feel free to send me an e-mail.
    I look forward to hearing from you! Awesome
    blog by the way!

  9. http://www.iphonetopsite.ru/index.php?a=stats&u=felipeumbagai Says:

    Sweet blog! I fοund it while searching оn Yahoo News.

    Do yoս have any suggestions on howw to get listed in Yahoo News?
    І’ve been tгying for a աhile butt І never seem
    to get there! Thank ʏou

  10. car purchasing company Says:

    Hеllo There. I fοund your weblog the use of msn. Thiѕ is ɑ гeally well written article.

    Ι will make sure to bookmark it and cߋme Ƅack to read extra ߋf yoour
    helpful info. Ƭhanks fօr tɦе post. I ԝill defіnitely return.

  11. schönheitschirurgie Says:

    Pretty nice post. I just stumbled upon your blog and wanted to
    say that I have really enjoyed surfing around your blog posts.
    After all I will be subscribing to your rss feed and I hope you write again soon!

  12. nasenkorrektur Says:

    Simply desire to say your article is as astounding.
    The clearness in your post is simply excellent and i can assume you’re an expert on this subject.
    Fine with your permission allow me to grab your feed to keep up to date with forthcoming post.
    Thanks a million and please keep up the rewarding work.

    • Chandra Dev Says:

      Thank you for posting sweet feedback.

  13. Danieloi Says:

    Interesting post )


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: