GridView: Dynamic Cascading DropDownList using JavaScript

Introduction

 
Recently, a user in the forums is asking how to populate one DropDownList based on a value selected from another DropDownList using JavaScript in ASP .NET. The question is quite interesting and thought I'd share the solution I've posted on that thread as a reference to others who might face the same problem.
 
Using the code
 
The scenario is that the DropDownList are inside a GridView TemplateFields and the user wanted to implement a cascading dropdown by dynamically creating the items on the fly without using jQuery, AJAX and database but purely JavaScript.
 
To implement that, let's add a GridView in the markup and setup some TemplateField columns with DropDownLists on it. Our ASPX markup should look something like this,
  1. <form id="form1" runat="server">    
  2.         <asp:GridView ID="GridView1" runat="server">    
  3.             <Columns>    
  4.                 <asp:TemplateField>    
  5.                     <ItemTemplate>    
  6.                         <asp:DropDownList ID="ddlStatus" runat="server" onchange="buildDropDown(this);">    
  7.                             <asp:ListItem></asp:ListItem>    
  8.                             <asp:ListItem Value="Ma">Ma</asp:ListItem>    
  9.                             <asp:ListItem Value="Mi">Mi</asp:ListItem>    
  10.                             <asp:ListItem Value="Others">Others</asp:ListItem>    
  11.                         </asp:DropDownList>    
  12.                     </ItemTemplate>    
  13.                 </asp:TemplateField>    
  14.                 <asp:TemplateField>    
  15.                     <ItemTemplate>    
  16.                         <asp:DropDownList ID="ddlReason" runat="server">    
  17.                         </asp:DropDownList>    
  18.                     </ItemTemplate>    
  19.                 </asp:TemplateField>    
  20.             </Columns>    
  21.         </asp:GridView>    
  22.     </form>   
There's nothing fancy in the markup above, except that we wired-up the onchange event to ddlStatus DropDownList.
 
Now let's create the JavaScript function. Here's the code block below,
  1. <script type="text/javascript">    
  2.       function buildDropDown(obj) {    
  3.           var status = obj.options[obj.selectedIndex].value;    
  4.           var row = obj.parentNode.parentNode;    
  5.           var rowIndex = row.rowIndex - 1;    
  6.           //you may need to change the index of cells value based on the location    
  7.           //of your ddlReason DropDownList    
  8.           var ddlReason = row.cells[1].getElementsByTagName('SELECT')[0];    
  9.   
  10.           switch (status) {    
  11.               case "Ma":    
  12.                   ddlReason.options[0] = new Option("OK""OK");    
  13.                   ddlReason.options[1] = new Option("Good""Good");    
  14.                   break;    
  15.               case "Mi":    
  16.                   ddlReason.options[0] = new Option("Data""Data");    
  17.                   ddlReason.options[1] = new Option("Resoure""Resoure");    
  18.                   ddlReason.options[2] = new Option("Time""Time");    
  19.                   break;    
  20.               case "Others":    
  21.                   ddlReason.options[0] = new Option("Some Item""Some Item");    
  22.                   break;    
  23.           }    
  24.       }    
  25.  </script>   
The function buildDropDown() takes a parameter obj. This parameter is a reference to a DropDownList control that is inside a GridView. Now let's take a look at what we've done there.
 
The status variable holds the selected value of the first DropDownList called ddlStatus.
 
The row variable serves as the naming container to which the DropDownList ddlStatus is located. Remember that a grid contains rows, so we need to determine which DropDown to populate.
 
The ddlReason variable represents the ddlReason DropDownList. Once we get the row, we can easily find an element in the cells using row.cells[1].getElementsByTagName('SELECT')[0]; That line means get the first SELECT element in the array that is within the second Column of the GridView.cells[1] represents the 2nd column. Keep in mind that index always starts at 0, so you may need to change the index based on your requirements.
 
The switch statement simply determines what items to be populated in the ddlReason DropDown based on the status value.
 
That's it! I hope someone find this post useful.