| | |
Binding dropdownlist using javascript
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 28
Reputation:
Solved Threads: 0
Hi!
I would be grateful be grateful if I get an answer to this problem......
I have 2 dropdownlists, one contains country names and the other contains states' names.
Now what I wish to do is that when i select a particular country from the first dropdownlist, its corresponding states should get filled up in the second dropdownlist.
This sounds very simple using Autopostback property, but what I wish is through Javascript.
That means, this should happen through javascript so that the page does not get refreshed........
Thanks
I would be grateful be grateful if I get an answer to this problem......
I have 2 dropdownlists, one contains country names and the other contains states' names.
Now what I wish to do is that when i select a particular country from the first dropdownlist, its corresponding states should get filled up in the second dropdownlist.
This sounds very simple using Autopostback property, but what I wish is through Javascript.
That means, this should happen through javascript so that the page does not get refreshed........
Thanks
•
•
Join Date: Sep 2008
Posts: 10
Reputation:
Solved Threads: 2
Well either way - if you can - try to get your data into the following format - I recommend downloading a JavaScript array of the countries (which will be smaller) - to your page - and then using ajas to retrieve the states for the country when it is selected... So you start with:
var coun = ["albania","algeria","belarus","belgium"...]
Then use the array to build the <option> elements of your select list. Iterate through each element of the array and use:
var o = new Option(text,value);
Then attach a function getStates() to the onChange event of the <select> element...
in getStates() get the value of the Countries <select> element - then use the country as a key to send an AJAX request to your server...
Have the server return the States for the selected country in JSON format (so you can eval it easily). put it in the following format.
{countryName:"United States",states:["alabama","alaska","arizona","arkansas","california","connecticut"....]}
so it is a JavaScript Object. (you can omit the country name and just output the array of state names if you want). Then just build a function to iterate the state names and add Options to your States <select> element.
This can all be made alot easier using something like jQuery... if you use that i can show you more complete code - hand coding in raw JS without a good library would be tedious...
Good luck!!
var coun = ["albania","algeria","belarus","belgium"...]
Then use the array to build the <option> elements of your select list. Iterate through each element of the array and use:
var o = new Option(text,value);
Then attach a function getStates() to the onChange event of the <select> element...
in getStates() get the value of the Countries <select> element - then use the country as a key to send an AJAX request to your server...
Have the server return the States for the selected country in JSON format (so you can eval it easily). put it in the following format.
{countryName:"United States",states:["alabama","alaska","arizona","arkansas","california","connecticut"....]}
so it is a JavaScript Object. (you can omit the country name and just output the array of state names if you want). Then just build a function to iterate the state names and add Options to your States <select> element.
This can all be made alot easier using something like jQuery... if you use that i can show you more complete code - hand coding in raw JS without a good library would be tedious...
Good luck!!
![]() |
Similar Threads
- Classic to .net migration question (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: how to select the data's from database as random
- Next Thread: Dataformatstring for dateTime
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol advice ajax alltypeofvideos appliances application asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions confirmationcodegeneration css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dialog dropdownlist dynamically edit expose feedback fileuploader fill flash form formatdecimal forms formview gridview gudi iframe iis javascript listbox login microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols parent radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security select sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webprogramming webservice xsl youareanotmemberofthedebuggerusers





