I came across a requirement recently where there were two ASP.NET DropDownList – Parent and Child. On selecting a Parent item, the Child items were displayed. However, there were some parents which did not have child elements. When such Parent Items were selected, the child DropDownList needed to be disabled.
Here’s how to do it using jQuery:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DropDownList</title>
<script type="text/javascript"
src="http://code.jquery.com/jquery-latest.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#DropDownList1").bind("change", function() {
if (this.value == "Item2") {
$("#DropDownList2").attr("disabled", true);
return true;
}
$("#DropDownList2").attr("disabled", false);
});
});
</script>
<style type="text/css">
div.divParent
{
width: 350px;
position: relative;
clear: both;
}
div.divOne
{
width: 50%;
position: relative;
float:left;
}
div.divTwo
{
width: 49%;
position: relative;
float:right;
}
</style>
</head>
<body>
<form runat="server">
<div class="divParent">
<div class="divOne">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Item1"></asp:ListItem>
<asp:ListItem Text="Item2"></asp:ListItem>
<asp:ListItem Text="Item3"></asp:ListItem>
</asp:DropDownList>
</div>
<div class="divTwo">
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Text="SubItem1"></asp:ListItem>
<asp:ListItem Text="SubItem3"></asp:ListItem>
<asp:ListItem Text="SubItem3"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</form>
</body>
</html>
In the example shown above, if “Item2” in the Parent DropDownList (DropDownList1) is selected, the Child DropDownList (DropDownList2) gets disabled.
Please note that the example focused on disabling the DropDownList and not on enabling the Master-Detail behavior.
Tweet
1 comment:
Kewl..
Post a Comment