Hai,
Thanks in advance for your great help..
I have added 2 dropdownlist boxes for Country and State. Based on the slected country, i have to load the states.
I used pagemethod to call the Serverside code of Binding states. But, its not loading..
Can anyone please rectify this.. If any doubt on this, pls ask
I herewith attached my sample code below:
.aspx
-------
<body onload="fnOnload();>
<script type="text/javascript" language="javascript" src="Scripts/Register.js"></script>
<form runat="server" id="frmMain">
<div id="reg">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
Register.js:
--------------
function LoadState() {
var CountryId = document.getElementById("ddlICountry").value;alert(CountryId);
PageMethods.ServerLoadState(CountryId, CallSuccess_LoadState, CallFailed_LoadState);
}
function CallSuccess_LoadState(res) {
var StateID = new Array();
var StateName = new Array();
var Collection = new Array();
var Collection_ID = new Array();
var Collection_Text = new Array();
Collection = res.split('~~~');
Collection_ID = Collection[0];
Collection_Text = Collection[1].split('|');
for (var i = 0; i < Collection_Text.length; i++) {
StateID[i] = i;
StateName[i] = Collection_Text[i].split(',');
}
document.getElementById("ddlIState").options.length = 0;
for (var i = 0; i < StateID.length; ++i) {
addOption(document.getElementById("ddlIState"), StateName[i], StateId[i]);
}
document.getElementById("ddlIState").disabled = false;
}
function CallFailed_LoadState(res, destCtrl) {
alert(res.get_message());
}
function addOption(selectbox, text, value) {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
.aspx.cs
-----------
[System.Web.Services.WebMethod]
public static string ServerLoadState(int CountryId)
{
SqlConnection con = null;
string Statelist = "";
string connection = ConfigurationManager.AppSettings["SoftProConnect"];
con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "sp_GetAllStates";
cmd.Parameters.AddWithValue("@CountryId", CountryId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 500;
cmd.Connection = con;
if (con.State == 0)
{
con.Open();
}
Statelist = cmd.ExecuteScalar().ToString();
return Statelist;
}
Calling the method in dropdownlist:
ddlICountry.Attributes.Add("onChange", "javascript:LoadState();");
[edit]Code blocks tidied up - OriginalGriff[/edit]