Using Jquery would be the best solution for such situation.even if you are not using it yet.
saving a postback would be worth it. Here is one example with State(parent) and County(child) relationship.
<asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlCounty" runat="server">
</asp:DropDownList>
Here is the jquery code to implement cascaded dropdown list.
$(document).ready(function () {
$("#<%= ddlState.ClientID %>").change(function () {
var sourceddl = "<%= ddlState.ClientID %>";
var stateid = $("#<%= ddlState.ClientID %> option:selected").val();
var Stateid = { Stateid: stateid };
$.ajax({
type: 'POST',
url: 'CacheSample.aspx/GetCounties',
data: JSON.stringify(Stateid),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (data.d) {
var options = [];
if (result.d) {
for (var i = 0; i < result.d.length; i++) {
options.push('<option value="',
result.d[i].countyID, '">',
result.d[i].countyName, '</option>');
}
$("#<%= ddlCounty.ClientID %>").html(options.join(''));
}
}
},
error: function () {
alert("Error! Try again...");
}
});
});
});
I am using a webmethod to retireive the Counties for a selected state.
[WebMethod]
public static County[] GetCounties(int Stateid)
{
County[] countiesArr = StatesCountyModel.GetCountyForState(Stateid).ToArray();
return countiesArr;
}
I guess it should help you. If you are new to Jquery please let me know. You just need to include few javascript files into your project and you can use this code.
Praveen