Perhaps you need to understand first the concepts and fundamentals of server controls and client-side functions in the context of ASP.NET. In essence, you should avoid when possible mixing your server-side implementation with client-side.
DropDownList
control is a server control and they are meant to be manipulated at the server-side, mixing it with
JavaScript
is "doable" however it will give you trouble when functionality gets complicated.
If you wanted to implement cascading
DropDownList
at the client, then you could easily do that with jQuery. Here's an example:
Cascading DropDownLists with jQuery and ASP.NET[
^]
However, if you really need JavaScript then there's no need for you to use the asp.net DropDownList control. Here's a quick example:
HTML:
<select name="optone" id="countrySel" size="1">
<option value="" selected="selected">Select Country</option>
</select>
<br>
<br>
<select name="opttwo" id="stateSel" size="1">
<option value="" selected="selected">Select State</option>
</select>
<br>
<br>
<select name="optthree" id="citySel" size="1">
<option value="" selected="selected">Select City</option>
</select>
JAVASCRIPT:
var countryStateInfo = {
"USA": {
"California": {
"Los Angeles": ["90001", "90002", "90003", "90004"],
"San Diego": ["92093", "92101"]
},
"Texas": {
"Dallas": ["75201", "75202"],
"Austin": ["73301", "73344"]
}
},
"India": {
"Assam": {
"Dispur": ["781005"],
"Guwahati": ["781030", "781030"]
},
"Gujarat": {
"Vadodara": ["390011", "390020"],
"Surat": ["395006", "395002"]
}
}
}
window.onload = function () {
var countrySel = document.getElementById("countrySel"),
stateSel = document.getElementById("stateSel"),
citySel = document.getElementById("citySel");
for (var country in countryStateInfo) {
countrySel.options[countrySel.options.length] = new Option(country, country);
}
countrySel.onchange = function () {
stateSel.length = 1;
citySel.length = 1;
if (this.selectedIndex < 1) {
stateSel.options[0].text = "Select State"
citySel.options[0].text = "Select City"
return;
}
stateSel.options[0].text = "Select State"
for (var state in countryStateInfo[this.value]) {
stateSel.options[stateSel.options.length] = new Option(state, state);
}
if (stateSel.options.length==2) {
stateSel.selectedIndex=1;
stateSel.onchange();
}
}
countrySel.onchange();
stateSel.onchange = function () {
citySel.length = 1;
if (this.selectedIndex < 1) {
citySel.options[0].text = "Please select State first"
return;
}
citySel.options[0].text = "Please select City"
for (var city in countryStateInfo[countrySel.value][this.value]) {
citySel.options[citySel.options.length] = new Option(city, city);
}
if (citySel.options.length==2) {
citySel.selectedIndex=1;
citySel.onchange();
}
}
}
If you want to use the
ASP.NET DropDownList
control then replace this:
var countrySel = document.getElementById("countrySel"),
stateSel = document.getElementById("stateSel"),
citySel = document.getElementById("citySel");
with this:
var countrySel = document.getElementById("<%= DDLCountry.ClientID %>"),
stateSel = document.getElementById("<%= DDLState.ClientID %>"),
citySel = document.getElementById("<%= DDLCity.ClientID %>");
and make sure to
turn-off AutoPostback
for all your
DropDownlist
.
You can also implement it using server-side with
DropDownList
. That really depends on you.