title: Show/Hide Checkbox in ASP.NET Datalist (nested)
body: <p>I have a nested ASP datalist that contains a label and a checkbox which is bound on page load.</p>
  
  <p>During the binding a parse a string into the datalist bind method. If this string equals the value <strong>1</strong> I would like the checkbox to become visible for each item in the nested datalist.</p>
  
  <p>And remain hidden is the value is <strong>not 1</strong>.</p>
  
  <p>However, the code compiles and runs. It detects the string value as 1 but then does not make the check box visible. What am I doing wrong? How can I solve this?</p>
  
  <p>Datalist:</p>
  
  <pre><code>&lt;asp:DataList runat="server" id="dgQuestionnaire" DataKeyField="QuestionID" CssClass="confirm"&gt;
      &lt;ItemTemplate&gt;
          &lt;div class="content_box" id="added_question"&gt;
              &lt;h3&gt;Question &lt;asp:Label ID="lblOrder" runat="server" Text='&lt;%# Container.ItemIndex  + 1 %&gt;'&gt;&lt;/asp:Label&gt;&lt;/h3&gt;
              &lt;p&gt;Question Type&lt;/p&gt;
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "QuestionText") %&gt;&lt;/p&gt;
              &lt;asp:DataList ID="nestedDataList" runat="server"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:CheckBox runat="server" ID="sampleCheckbox" Visible="false" /&gt;
                      &lt;p class="confirm_answer new"&gt;&lt;%# DataBinder.Eval(Container.DataItem, "AnswerTitle") %&gt;&lt;/p&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:DataList&gt;
          &lt;/div&gt;    
      &lt;/ItemTemplate&gt; 
  &lt;/asp:DataList&gt;
  </code></pre>
  
  <p>And here is the code behind:</p>
  
  <pre><code>public void BindParentDataList(int questionnaireID)
  {
      // populate the datalist with items from the datatable.
      SendData = new OsqarSQL();
      DT = SendData.GetQuestionNameDataList(questionnaireID);
      dgQuestionnaire.DataSource = DT;
      dgQuestionnaire.DataBind();
  
      // each item in the datalist call BindNestedDataList method.
      foreach (DataListItem Item in dgQuestionnaire.Items)
      {
          BindNestedDataList(Item.ItemIndex, DT.Rows[Item.ItemIndex][2].ToString());
      }
  }
  
  public void BindNestedDataList(int ItemIndex, string _questionType)
  {
      // get questionID value for the current datalist item and populate the answers for the question.
      int questionID = Convert.ToInt32(dgQuestionnaire.DataKeys[ItemIndex]);
      DT = SendData.GetAnswerTitle(questionID);
      DataList nestedDataList = (DataList)dgQuestionnaire.Items[ItemIndex].FindControl("nestedDataList");
  
      if(_questionType == "1")
      {                
          foreach (DataListItem Item in nestedDataList.Items)
          {
              CheckBox checkbox = (CheckBox)Item.FindControl("sampleCheckbox");
              checkbox.Visible = true;
          } 
      }
      nestedDataList.DataSource = DT;
      nestedDataList.DataBind();
  }
  </code></pre>