jQuery and ASP.NET

June 29, 2009

Disable Child DropDownList On Selection of Parent DropDownList Using jQuery




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.



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

1 Response to "Disable Child DropDownList On Selection of Parent DropDownList Using jQuery"
  1. Boslo said...
    July 1, 2009 9:42 PM

    Kewl..

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions