I have a parent dropdownlist(named ddlCountry here after) on selected index change of which i am making a ajax call to present relevant data in a cascaded dropdown(Like if i select a country in the parent dropdown, it is going to give me the state names in ddlState.)
Alongside with it,I am cloning ddlCountry and ddlState with the help of the clone() function in my btnClone button. I want my dynamically generated controls to exhibit the same behavior. Like if select an item in ddlCountry_1, i should have the correct state name in ddlState_1 and so on...
Here is my code:
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnClone" Text="Clone" runat="server" />
</div>
<br />
<br />
<table>
<tr>
<td>Cateogry:
</td>
<td>
<div>
<asp:DropDownList ID="ddlCountryList" runat="server" class="ddlCountryClass"></asp:DropDownList>
</div>
</td>
<td>SubCategory:
</td>
<td>
<div>
<asp:DropDownList ID="ddlStateList" runat="server" class="ddlStateClass"></asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<div id="target">
</div>
</td>
<td>
<div id="target2">
</div>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
$(function () {
$("[id*=btnClone]").bind("click", function () {
var index = $("#target select").length + 1;
//Clone the DropDownList
var ddl = $("[id$=ddlCountryList]").clone();
//Set the ID and Name
ddl.attr("id", "ddlCountryList_" + index);
ddl.attr("name", "ddlCountryList_" + index);
//[OPTIONAL] Copy the selected value
var selectedValue = $("[id$=ddlCountryList] option:selected").val();
ddl.find("option[value = '" + selectedValue + "']").attr("selected", "selected");
//Append to the DIV.
$("#target").append(ddl);
$("#target").append("<br /><br />");
return false;
});
});
$(function () {
$("[id*=btnclone]").bind("click", function () {
var index = $("#target2 select").length + 1;
//clone the dropdownlist
var ddl = $("[id$=ddlstatelist]").clone();
//set the id and name
ddl.attr("id", "ddlstatelist_" + index);
ddl.attr("name", "ddlstatelist_" + index);
//[optional] copy the selected value
var selectedvalue = $("[id$=ddlstatelist] option:selected").val();
ddl.find("option[value = '" + selectedvalue + "']").attr("selected", "selected");
//append to the div.
$("#target2").append(ddl);
$("#target2").append("<br /><br />");
return false;
});
});
// Make Ajax call to fetch the state values.
$(function () {
$('#ddlStateList').attr('disabled', 'disabled');
$('#ddlStateList').append('<option selected="selected" value="0">Select State</option>');
$('#ddlCountryList').change(function () {
var country = $('#ddlCountryList').val()
$('#ddlStateList').removeAttr("disabled");
$.ajax({
type: "POST",
url: "Default.aspx/BindStates",
data: "{'country':'" + country + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var j = jQuery.parseJSON(msg.d);
var options;
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'
}
$('#ddlStateList').html(options)
},
error: function (data) {
alert('Something Went Wrong')
}
});
});
});
</script>