Try this out (By Pallav Deshmukh pallavdeshmukh@rediffmail.com)
In your web.config file add following
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
Add following script to your aspx page inside <head>
<script language="javascript" type="text/javascript">
function populateState(Country)
{
var cid = Country.options[Country.selectedIndex].value;
Country_State.state1(cid,call_back);
}
function call_back(response)
{
var dataset=response.value;
if (dataset == null || typeof(dataset) != "object")
{
return;
}
var State = document.getElementById("StateDDL");
State.options.length = 0;
for(index=0;index<dataset.Tables[0].Rows.length;index++)
{
var op = new Option(dataset.Tables[0].Rows[index].State_Name,dataset.Tables[0].Rows[index].State_ID);
op.value = dataset.Tables[0].Rows[index].State_ID;
State.options.add(op);
}
}
</script>
where your page contains following
<asp:DropDownList ID="CountryDDL" runat="server" OnChange="populateState(this)">
</asp:DropDownList><br />
<asp:DropDownList ID="StateDDL" runat="server">
</asp:DropDownList>
Add following code to your Aspx.cs page
using Ajax;
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Country_St ate));
if (!IsPostBack)
{
DataSet TempDataSet = new DataSet();
SqlConnection _TempConnection = new SqlConnection(System.Configuration.ConfigurationMa nager.AppSettings["ConnectionString"]);
SqlCommand _TempCommand = new SqlCommand("Select * from Country order by sort_order", _TempConnection);
SqlDataAdapter _TempAdapter = new SqlDataAdapter(_TempCommand);
_TempAdapter.Fill(TempDataSet);
CountryDDL.DataSource = TempDataSet.Tables[0];
CountryDDL.DataTextField = TempDataSet.Tables[0].Columns["Country_Name"].ToString();
CountryDDL.DataValueField = TempDataSet.Tables[0].Columns["Country_ID"].ToString();
CountryDDL.DataBind();
}
//CountryDDL.Attributes.Add("OnChange", "return populateState()");
}
[Ajax.AjaxMethod()]
public DataSet state1(int country)
{
//set connection
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationMa nager.AppSettings["ConnectionString"]);
SqlDataAdapter da=new SqlDataAdapter("select Distinct state_ID,State_Name from State where country_id="+country , conn);
DataSet ds=new DataSet();
da.Fill(ds);
return ds;
}
You must add refrence of Ajex.dll in your Application. This will solve your problem.
----------------------------------------------------------------------------------------------------------------
write the javascript function in head section for onchange event for dropdownlist or combobox
[code]
ex:
<asp DropDownList id=country........... onchange="state();">
<asp DropDownList id=state........... onchange="district();">
<asp DropDownList id=district........... ">
in codebehind write ajax method which returns dataset
[Ajax.AjaxMethod()]
public DataSet state1(string country)
{
//set connection
SqlConection conn=new SqlConnection(" ")
SqlDataAdapter da=new SqlDataAdapter("select Distinct state from tablename where country='"+country+"'",conn);
Dataset ds=new Dataset();
da.Fill(ds);
return ds;
}
in headsection write javascript for change
function state()
{
var response=_Default.state1(document.getElementId("co untry").value,call_back);//_default is the class name ie the codebehind classname
//the response contains the dataset
}
function call_back(response)
{
var dataset=response.value;
if(dataset.Tables[0].Rows.length==0) //Clearing the Dropdownlist when changed
{
{
var ddCategories = document.getElementById("<%=state.ClientID%>");
ddCategories.options.length = 0;
var ddCategories = document.getElementById("<%=district.ClientID%>");
ddCategories.options.length = 0;
}
else
{
for(index=0;index<dataset.Tables[0].Rows.length;index++)
{
str=dataset.Tables[0].Rows[index].state;
document.getElementId("state").options[index]=new Option(str,str);
}
}
similarly write for the change in onchange=district()
[code]
and also check the refrences
http://www.developerfusion.co.uk/show/4704/2
http://www.codeproject.com/Ajax/IntroAjaxASPNET.asp#Using_ASP.NET_Callbacks_for_Aj ax
for javascript
http://www.plus2net.com/javascript_tutorial/javascript_tutorial.php
i think it may help u