473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ASP.NET WITH Ajax

How to bind combo boxs from database using Ajax.Any body help me regarding this.(With out page refresh)
Example criteria: Based on country combo box populate STATE combobox
& Based on STATE combo box populate DISTRICT combobox...
Oct 18 '06 #1
3 4668
Please visit The Microsoft Atlas Page to make AJAX Web Aplications Easy as Windows Forms...
Oct 18 '06 #2
vee10
141 100+
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






How to bind combo boxs from database using Ajax.Any body help me regarding this.(With out page refresh)
Example criteria: Based on country combo box populate STATE combobox
& Based on STATE combo box populate DISTRICT combobox...
Oct 19 '06 #3
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
Mar 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Yarco | last post by:
I want to use "Ajax" to create my web for hobby. But i don't know whether "Ajax" is mature... And what about with php? Someone have experience on it? ....
4
by: bobzimuta | last post by:
I'm creating a simple AJAX library. It's an object that will return an array containing the response text or xml. I'm trying to find a way to assign the response as a property of the object, but...
0
by: melledge | last post by:
Ajax Developers' Day added to XTech 2006 agenda XTech 2006 - 17-19 May - Hotel Grand Krasnopolsky - Amsterdam, The Netherlands
0
by: melledge | last post by:
Ajax Developers' Day to Kick Off XTech 2006 Conference Industry experts offer insight into next generation of the Web ALEXANDRIA, VIRGINIA, USA - April 25, 2006 - In response to the rapidly...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
10
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
controlsPlease could some of you here post some of your live examples of AJAX (esp drag panels, collapsable panels, and popup menu.) (It's one thing to talk about how great something is, but it's...
2
by: soni2926 | last post by:
hi, does anyone know of any good books on ajax and asp.net, one that teaches ajax itself before jumping in atlas? I wanted to get an understanding of ajax and how to use it, most books i've seen...
1
by: shaunwo | last post by:
I'm an AJAX / DOM Novice (at best) and trying to figure out how to write the value to a couple input fields. I don't remember exactly where I got the ajax.js file I'm using from (went to the website...
11
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have run into a situation that if a page/tab that uses the Ajax toolkit (using .net version 3.5) is closed before the Ajax enable controls complete loading, then IE locks up. Does it in both IE7...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.