title: How to make multiple DropDownList change based on selection from other DropDownList
body: <p>I have the following ASP.net code which I am using to filter my <code>GridView</code>:</p>
  
  <pre><code>&lt;asp:UpdatePanel runat="server" ClientIDMode="Static" ID="TasksUpdatePanel" UpdateMode="Conditional"&gt;
      &lt;ContentTemplate&gt;
          &lt;table style="width: 100%; padding: 0; margin: 0; border: 0; border-spacing: 0; border-collapse: separate;" class="GridviewTable"&gt;
              &lt;tr&gt;
                  &lt;td style="width: 25%;"&gt;
                      &lt;asp:DropDownList ID="ddlTaskName" CssClass="chosen-select" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlTaskName_onSelectIndexChanged"&gt;
                          &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                      &lt;asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="&lt;%$ ConnectionStrings:gvConnString %&gt;" SelectCommand="SELECT DISTINCT [ATTR2739] 'Task Name' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) WHERE CT.ACTIVESTATUS = 0"&gt;&lt;/asp:SqlDataSource&gt;
                  &lt;/td&gt;
                  &lt;td style="width: 20%;"&gt;
                      &lt;asp:DropDownList ID="ddlService" CssClass="chosen-select" DataSourceID="dsPopulateService" AutoPostBack="true" DataValueField="Service" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlService_onSelectIndexChanged"&gt;
                          &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                      &lt;asp:SqlDataSource ID="dsPopulateService" runat="server" ConnectionString="&lt;%$ ConnectionStrings:gvConnString %&gt;" SelectCommand="SELECT DISTINCT [ATTR2846] 'Service' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) WHERE CT.ACTIVESTATUS = 0"&gt;&lt;/asp:SqlDataSource&gt;
                  &lt;/td&gt;
                  &lt;td style="width: 11%;"&gt;
                      &lt;asp:DropDownList ID="ddlStatus" CssClass="chosen-select" DataSourceID="dsPopulateStatus" AutoPostBack="true" DataValueField="Status" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlStatus_onSelectIndexChanged"&gt;
                          &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                      &lt;asp:SqlDataSource ID="dsPopulateStatus" runat="server" ConnectionString="&lt;%$ ConnectionStrings:gvConnString %&gt;" SelectCommand="SELECT DISTINCT [ATTR2812] 'Status' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) WHERE CT.ACTIVESTATUS = 0"&gt;&lt;/asp:SqlDataSource&gt;
                  &lt;/td&gt;
                  &lt;td style="width: 14%;"&gt;
                      &lt;asp:DropDownList ID="ddlDueDate" CssClass="chosen-select" DataSourceID="dsPopulateDueDate" AutoPostBack="true" DataValueField="Due Date" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlDueDate_onSelectIndexChanged"&gt;
                          &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                      &lt;asp:SqlDataSource ID="dsPopulateDueDate" runat="server" ConnectionString="&lt;%$ ConnectionStrings:gvConnString %&gt;" SelectCommand="SELECT DISTINCT CONVERT(VARCHAR(14), [ATTR2752], 110) 'Due Date' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) WHERE CT.ACTIVESTATUS = 0"&gt;&lt;/asp:SqlDataSource&gt;
                  &lt;/td&gt;
                  &lt;td style="width: 15%;"&gt;
                      &lt;asp:DropDownList ID="ddlOwner" CssClass="chosen-select" DataSourceID="dsPopulateOwner" AutoPostBack="true" DataValueField="Owner" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlOwner_onSelectIndexChanged"&gt;
                          &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                      &lt;asp:SqlDataSource ID="dsPopulateOwner" runat="server" ConnectionString="&lt;%$ ConnectionStrings:gvConnString %&gt;" SelectCommand="SELECT DISTINCT [REALNAME] 'Owner' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) WHERE CT.ACTIVESTATUS = 0"&gt;&lt;/asp:SqlDataSource&gt;
                  &lt;/td&gt;
                  &lt;td style="width: 15%;"&gt;
                      &lt;asp:DropDownList ID="ddlClient" CssClass="chosen-select" DataSourceID="dsPopulateClient" AutoPostBack="true" DataValueField="Client" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlClient_onSelectIndexChanged"&gt;
                          &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                      &lt;asp:SqlDataSource ID="dsPopulateClient" runat="server" ConnectionString="&lt;%$ ConnectionStrings:gvConnString %&gt;" SelectCommand="SELECT DISTINCT [ATTR2799] 'Client' FROM HSI.RMOBJECTINSTANCE1224 CT INNER JOIN HSI.RMOBJECTINSTANCE1232 S ON CT.ATTR2846 = S.ATTR2821 INNER JOIN HSI.USERACCOUNT UA ON S.FK2852 = (UA.USERNUM * -1) WHERE CT.ACTIVESTATUS = 0"&gt;&lt;/asp:SqlDataSource&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated"&gt;
              &lt;Columns&gt;
                  &lt;asp:HyperLinkField Target="_blank" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-Width="25%" ItemStyle-CssClass="taskTableColumn" /&gt;
                  &lt;asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-Width="20%" ItemStyle-CssClass="taskTableColumn" /&gt;
                  &lt;asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-Width="10%" ItemStyle-CssClass="taskTableColumn" /&gt;
                  &lt;asp:BoundField DataField="Due Date" HeaderText="Due Date" SortExpression="Due Date" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" /&gt;
                  &lt;asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" /&gt;
                  &lt;asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" /&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
      &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>My code-behind for the ASP.net page above:</p>
  
  <pre><code>protected void ddlTaskName_onSelectIndexChanged(object sender, EventArgs e)
  {
      string query = "";
      string strText = ddlTaskName.SelectedItem.Text;
      ddlService.SelectedIndex = 0;
      ddlStatus.SelectedIndex = 0;
      ddlDueDate.SelectedIndex = 0;
      ddlClient.SelectedIndex = 0;
      ddlOwner.SelectedIndex = 0;
      //MessageBox.Show(strText);
  
      DataTable taskData = new DataTable();
      if (strText == "All")
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0";
      }
      else
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2739 = '" + strText + "'";
      }
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
  
              DataSet myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              DataView myDataView = new DataView();
              myDataView = myDataSet.Tables[0].DefaultView;
  
              if (ViewState["sortExp"].ToString() != string.Empty)
              {
                  //MessageBox.Show(sortExp);
                  //MessageBox.Show(sortDir);
                  myDataView.Sort = string.Format("{0} {1}", ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString());
              }
  
              yourTasksGV.DataSource = myDataView;
              yourTasksGV.DataBind();
  
              TasksUpdatePanel.Update();
  
              conn.Close();
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  protected void ddlOwner_onSelectIndexChanged(object sender, EventArgs e)
  {
      string query = "";
      string strText = ddlOwner.SelectedItem.Text;
      ddlService.SelectedIndex = 0;
      ddlStatus.SelectedIndex = 0;
      ddlDueDate.SelectedIndex = 0;
      ddlClient.SelectedIndex = 0;
      ddlTaskName.SelectedIndex = 0;
      //MessageBox.Show(strText);
  
      DataTable taskData = new DataTable();
      if (strText == "All")
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0";
      }
      else
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND UA.REALNAME = '" + strText + "'";
      }
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
  
              DataSet myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              DataView myDataView = new DataView();
              myDataView = myDataSet.Tables[0].DefaultView;
  
              if (ViewState["sortExp"].ToString() != string.Empty)
              {
                  //MessageBox.Show(sortExp);
                  //MessageBox.Show(sortDir);
                  myDataView.Sort = string.Format("{0} {1}", ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString());
              }
  
              yourTasksGV.DataSource = myDataView;
              yourTasksGV.DataBind();
  
              TasksUpdatePanel.Update();
  
              conn.Close();
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  protected void ddlService_onSelectIndexChanged(object sender, EventArgs e)
  {
      string query = "";
      string strText = ddlService.SelectedItem.Text;
      ddlTaskName.SelectedIndex = 0;
      ddlStatus.SelectedIndex = 0;
      ddlDueDate.SelectedIndex = 0;
      ddlClient.SelectedIndex = 0;
      ddlOwner.SelectedIndex = 0;
      //MessageBox.Show(strText);
  
      DataTable taskData = new DataTable();
      if (strText == "All")
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0";
      }
      else
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2846 = '" + strText + "'";
      }
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
  
              DataSet myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              DataView myDataView = new DataView();
              myDataView = myDataSet.Tables[0].DefaultView;
  
              if (ViewState["sortExp"].ToString() != string.Empty)
              {
                  //MessageBox.Show(sortExp);
                  //MessageBox.Show(sortDir);
                  myDataView.Sort = string.Format("{0} {1}", ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString());
              }
  
              yourTasksGV.DataSource = myDataView;
              yourTasksGV.DataBind();
  
              TasksUpdatePanel.Update();
  
              conn.Close();
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  protected void ddlStatus_onSelectIndexChanged(object sender, EventArgs e)
  {
      string query = "";
      string strText = ddlStatus.SelectedItem.Text;
      ddlService.SelectedIndex = 0;
      ddlTaskName.SelectedIndex = 0;
      ddlDueDate.SelectedIndex = 0;
      ddlClient.SelectedIndex = 0;
      ddlOwner.SelectedIndex = 0;
      //MessageBox.Show(strText);
  
      DataTable taskData = new DataTable();
      if (strText == "All")
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0";
      }
      else
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2812 = '" + strText + "'";
      }
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
  
              DataSet myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              DataView myDataView = new DataView();
              myDataView = myDataSet.Tables[0].DefaultView;
  
              if (ViewState["sortExp"].ToString() != string.Empty)
              {
                  //MessageBox.Show(sortExp);
                  //MessageBox.Show(sortDir);
                  myDataView.Sort = string.Format("{0} {1}", ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString());
              }
  
              yourTasksGV.DataSource = myDataView;
              yourTasksGV.DataBind();
  
              TasksUpdatePanel.Update();
  
              conn.Close();
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  protected void ddlDueDate_onSelectIndexChanged(object sender, EventArgs e)
  {
      string query = "";
      string strText = ddlDueDate.SelectedItem.Text;
      ddlService.SelectedIndex = 0;
      ddlStatus.SelectedIndex = 0;
      ddlTaskName.SelectedIndex = 0;
      ddlClient.SelectedIndex = 0;
      ddlOwner.SelectedIndex = 0;
      //MessageBox.Show(strText);
  
      DataTable taskData = new DataTable();
      if (strText == "All")
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0";
      }
      else
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CONVERT(VARCHAR(14), CT.ATTR2752, 110) = '" + strText + "'";
      }
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
  
              DataSet myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              DataView myDataView = new DataView();
              myDataView = myDataSet.Tables[0].DefaultView;
  
              if (ViewState["sortExp"].ToString() != string.Empty)
              {
                  //MessageBox.Show(sortExp);
                  //MessageBox.Show(sortDir);
                  myDataView.Sort = string.Format("{0} {1}", ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString());
              }
  
              yourTasksGV.DataSource = myDataView;
              yourTasksGV.DataBind();
  
              TasksUpdatePanel.Update();
  
              conn.Close();
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  protected void ddlClient_onSelectIndexChanged(object sender, EventArgs e)
  {
      string query = "";
      string strText = ddlClient.SelectedItem.Text;
      ddlService.SelectedIndex = 0;
      ddlStatus.SelectedIndex = 0;
      ddlDueDate.SelectedIndex = 0;
      ddlTaskName.SelectedIndex = 0;
      ddlOwner.SelectedIndex = 0;
      //MessageBox.Show(strText);
  
      DataTable taskData = new DataTable();
      if (strText == "All")
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0";
      }
      else
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2799 = '" + strText + "'";
      }
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
  
              DataSet myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              DataView myDataView = new DataView();
              myDataView = myDataSet.Tables[0].DefaultView;
  
              if (ViewState["sortExp"].ToString() != string.Empty)
              {
                  //MessageBox.Show(sortExp);
                  //MessageBox.Show(sortDir);
                  myDataView.Sort = string.Format("{0} {1}", ViewState["sortExp"].ToString(), ViewState["sortOrder"].ToString());
              }
  
              yourTasksGV.DataSource = myDataView;
              yourTasksGV.DataBind();
  
              TasksUpdatePanel.Update();
  
              conn.Close();
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  </code></pre>
  
  <p>The function which does the filtering of the <code>GridView</code>:</p>
  
  <pre><code>public void PullData(string sortExp, string sortDir)
  {
      string query = "";
      DataTable taskData = new DataTable();
      connString = ""; //the connection string is here
      if (ddlTaskName.SelectedIndex == 0 &amp;&amp; ddlService.SelectedIndex == 0 &amp;&amp; ddlStatus.SelectedIndex == 0 &amp;&amp; ddlDueDate.SelectedIndex == 0 &amp;&amp; ddlOwner.SelectedIndex == 0 &amp;&amp; ddlClient.SelectedIndex == 0)
      {
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0";
      }
      else if (ddlTaskName.SelectedIndex &gt; 0)
      {
          string strText = ddlTaskName.SelectedItem.Text;
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2739 = '" + strText + "'";
      }
      else if (ddlService.SelectedIndex &gt; 0)
      {
          string strText = ddlService.SelectedItem.Text;
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2846 = '" + strText + "'";
      }
      else if (ddlStatus.SelectedIndex &gt; 0)
      {
          string strText = ddlStatus.SelectedItem.Text;
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2812 = '" + strText + "'";
      }
      else if (ddlDueDate.SelectedIndex &gt; 0)
      {
          string strText = ddlDueDate.SelectedItem.Text;
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CONVERT(VARCHAR(14), CT.ATTR2752, 110) = '" + strText + "'";
      }
      else if (ddlOwner.SelectedIndex &gt; 0)
      {
          string strText = ddlClient.SelectedItem.Text;
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND UA.REALNAME = '" + strText + "'";
      }
      else if (ddlClient.SelectedIndex &gt; 0)
      {
          string strText = ddlClient.SelectedItem.Text;
          query = strMainQuery + " WHERE CT.ACTIVESTATUS = 0 AND CT.ATTR2799 = '" + strText + "'";
      }
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
  
              DataSet myDataSet = new DataSet();
              da.Fill(myDataSet);
  
              DataView myDataView = new DataView();
              myDataView = myDataSet.Tables[0].DefaultView;
  
              if (sortExp != string.Empty)
              {
                  //MessageBox.Show(sortExp);
                  //MessageBox.Show(sortDir);
                  myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
              }
  
              yourTasksGV.DataSource = myDataView;
              yourTasksGV.DataBind();
  
              conn.Close();
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  </code></pre>
  
  <p>What I want to do is, when a user choose an option from one of the <code>DropDownList</code>'s the other <code>DropDownList</code> will change to the values for the filtered <code>GridView</code> but because the <code>DataSource</code> is hard coded in, I am not able to do it.</p>
  
  <p>How do I filter each of the <code>DropDownList</code>'s from the filtered <code>GridView</code> once any selection of the <code>DropDownList</code> has been made?</p>