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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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><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>
grt
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!
Thank for your kind words and motivating me to do good work.
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.
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!
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!
Thesse aare in fsct fantastic iideas in abot blogging.
Youu hawve ttouched some gkod thins here.
Any wayy keep upp wrinting.
Thank you.
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!
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!
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
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.
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!
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.
Thank you for posting sweet feedback.
Interesting post )