dropdownlistboxes in Ajax with asp.net 2.0
I have given reference of Ajax.dll in my project. there are 2 dropdownlist boxes (one is for country and other one is for State) in one form. they are interrelated means if i select any country, depends on that i will get states's list in my other dropdownlistbox.
This is code is working...
I writed down code in sample.aspx page:
<script language="javascript" type="text/javascript">
<!--
function ClientGetStates(ddlCountry)
{
var countryId = ddlCountry.options[ddlCountry.selectedIndex].value;
if (countryId > 0)
{
PresentationLayer_JobSeeker_frmJobSeekerRegistrati on.GetStates(countryId,LoadStates_CallBack
);
}
}
function LoadStates_CallBack(response)
{
if (response.error != null)
{
alert(response.error);
return;
}
var states = response.value;
if (states == null || typeof(states) != "object")
{
return;
}
var statesList = document.getElementById("ddlState");
statesList.options.length=0;
for (var i = 0; i < states.length; ++i)
{
statesList.options[statesList.options.length] = new
Option(states[i].State_Name, states[i].State_ID);
}
}
// -->
</script>
<select id="ddlCountry" language="javascript" onchange="ClientGetStates(this)"
runat="server"> </select>
<select id="ddlState" datatextfield="State_Name" datavaluefield="State_ID"
language="javascript"></select>
In Sample.aspx.cs File:
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Sample));
}
[Ajax.AjaxMethod()]
public static DataView GetStates(Int32 iCountryIndex)
{
System.Data.DataView dv = new
System.Data.DataView(((DataTable)ParamState.ShowSe lected(iCountryIndex).Tables[0]));
return dv;
}
I want to run ddlState on server means (runat=server). at that time i m writing this code but this code is not running. can u suggest if i want to run this further code want to be run that what should i have to do.
This is not working ....
<script language="javascript" type="text/javascript">
<!--
function ClientGetStates(ddlCountry)
{
var countryId = ddlCountry.options[ddlCountry.selectedIndex].value;
if (countryId > 0)
{
PresentationLayer_JobSeeker_frmJobSeekerRegistrati on.GetStates(countryId,LoadStates_CallBack
);
}
}
function LoadStates_CallBack(response)
{
if(response.value == true)
{
alert("You have changed");
}
}
// -->
</script>
<select id="ddlCountry" language="javascript" onchange="ClientGetStates(this)"
runat="server"> </select>
<select id="ddlState" datatextfield="State_Name" datavaluefield="State_ID"
runat="server"> </select>
In Sample.aspx.cs File:
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Sample));
}
[Ajax.AjaxMethod()]
public static bool GetStates(Int32 iCountryIndex)
{
ddlState.Items.Clear();
ddlState.DataSource = ParamState.ShowSelected(iCountryIndex).Tables[0].DefaultView;
ddlState.DataTextField = "State_Name";
ddlState.DataValueField = "State_ID";
ddlState.DataBind();
return true;
}
|