Load Dropdownlist using webservice
Key Code:
In class File:
Namespace
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
using System.Configuration;
Code:
[WebMethod]
public static List<clsDropDownList> GetState(string CountryId)
{
List<clsDropDownList> obj = new List<clsDropDownList>();
string query = "SELECT State_Id, State_Name FROM Mst_State where Country_Id='" + CountryId + "' ORDER BY State_Id";
string constr = ConfigurationManager.ConnectionStrings["conn_scert"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
obj.Add(new clsDropDownList
{
Value = sdr["State_Id"].ToString(),
Text = sdr["State_Name"].ToString()
});
}
}
con.Close();
return obj;
}
}
}
public class clsDropDownList
{
public string Value { get; set; }
public string Text { get; set; }
}
In ASPX Page:
create a form.
<form id="form1" runat="server">
<div>
<table>
<tr>
<td> Select Country</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="01">India</asp:ListItem>
<asp:ListItem Value="02">Srilanka</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td> Select State</td>
<td>
<asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>
Javascript:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// Ducument .redy
$("#<%= ddlCountry.ClientID %>").change(function () {
var CountryId = $("#<%= ddlCountry.ClientID %>").val();
//// $.ajax
$.ajax({
type: "POST",
url: "EntryForm.aspx/GetState",
data: '{CountryId:"' + CountryId + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#<%= ddlState.ClientID %>").empty().append('<option selected="selected" value="0">Please select</option>');
$.each(response.d, function () {
$("#<%= ddlState.ClientID %>").append($("<option></option>").val(this['Value']).html(this['Text']));
});
},
failure: function (response) {
alert(response.d);
}
});
/// $.ajax
});
$("#<%= btnSubmit.ClientID %>").click(function () {
var CountryId = $("#<%= ddlCountry.ClientID %>").val();
alert(CountryId);
});
});
</script>
Details Code:
In class File:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
using System.Configuration;
public partial class Forms_EntryForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<clsDropDownList> GetState(string CountryId)
{
List<clsDropDownList> obj = new List<clsDropDownList>();
string query = "SELECT State_Id, State_Name FROM Mst_State where Country_Id='" + CountryId + "' ORDER BY State_Id";
string constr = ConfigurationManager.ConnectionStrings["conn_scert"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
obj.Add(new clsDropDownList
{
Value = sdr["State_Id"].ToString(),
Text = sdr["State_Name"].ToString()
});
}
}
con.Close();
return obj;
}
}
}
public class clsDropDownList
{
public string Value { get; set; }
public string Text { get; set; }
}
}
In Aspx File:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EntryForm.aspx.cs" Inherits="Forms_EntryForm" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td> Select Country</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="01">India</asp:ListItem>
<asp:ListItem Value="02">Srilanka</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td> Select State</td>
<td>
<asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
<%--<script src="../JS/ajax.googleapis.com.ajax.libs.jquery.1.8.3.jquery.min.js" type="text/javascript"></script>--%>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// Ducument .redy
$("#<%= ddlCountry.ClientID %>").change(function () {
var CountryId = $("#<%= ddlCountry.ClientID %>").val();
//// $.ajax
$.ajax({
type: "POST",
url: "EntryForm.aspx/GetState",
data: '{CountryId:"' + CountryId + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#<%= ddlState.ClientID %>").empty().append('<option selected="selected" value="0">Please select</option>');
$.each(response.d, function () {
$("#<%= ddlState.ClientID %>").append($("<option></option>").val(this['Value']).html(this['Text']));
});
},
failure: function (response) {
alert(response.d);
}
});
/// $.ajax
});
$("#<%= btnSubmit.ClientID %>").click(function () {
var CountryId = $("#<%= ddlCountry.ClientID %>").val();
alert(CountryId);
});
});
</script>
</form>
</body>
</html>