title: Dynamically Create Controls Based On Database field Asp.net
body: <p>I have a database table that looks like so:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">fieldid</th>
  <th>form_id</th>
  <th style="text-align: center;">control_type</th>
  <th>parentid</th>
  <th>order_number</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">1</td>
  <td>1</td>
  <td style="text-align: center;">panel</td>
  <td>0</td>
  <td>0</td>
  </tr>
  <tr>
  <td style="text-align: center;">2</td>
  <td>1</td>
  <td style="text-align: center;">textbox</td>
  <td>0</td>
  <td>1</td>
  </tr>
  <tr>
  <td style="text-align: center;">3</td>
  <td>1</td>
  <td style="text-align: center;">checkbox</td>
  <td>0</td>
  <td>0</td>
  </tr>
  <tr>
  <td style="text-align: center;">4</td>
  <td>1</td>
  <td style="text-align: center;">panel</td>
  <td>1</td>
  <td>0</td>
  </tr>
  <tr>
  <td style="text-align: center;">1</td>
  <td>1</td>
  <td style="text-align: center;">checkbox</td>
  <td>4</td>
  <td>0</td>
  </tr>
  <tr>
  <td style="text-align: center;">1</td>
  <td>1</td>
  <td style="text-align: center;">textbox</td>
  <td>4</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>There are other fields that I want to attach to this table but I want to dynamically create controls within the associated panel by using  recursion.</p>
  <p>I call the load_form with two parameters from the page_load event
  my page has one panel in it called PnlMain and this panel is where I want the dynamically loaded controls placed within.</p>
  <p>can anyone help me out with how to load only the controls that are associated with the &quot;panels&quot; with the fieldid that's equal to the associated controls parentid</p>
  <p>Here is my code behind.</p>
  <pre><code>Sub load_form(c As Control, Optional parent_id As Integer = 0)
      i = i + 1
      Dim frmid As Integer
      If Request(&quot;formid&quot;) Is Nothing Then
          frmid = 1
      Else
          frmid = Request(&quot;formid&quot;)
      End If
  
  
      Dim dt As DataTable
      Dim sql As String = &quot;&quot;
      If parent_id = 0 Then
          sql = &quot;SELECT * from form_fields where form_id=&quot; &amp; frmid &amp; &quot; order by parentid, order_number&quot;
          dt = GetData(sql)
      Else
          sql = &quot;SELECT * from form_fields where form_id=&quot; &amp; frmid &amp; &quot; and parentid=&quot; &amp; parent_id &amp; &quot;  order by parentid, order_number&quot;
          dt = GetData(sql)
      End If
  
      If dt.Rows.Count &gt; 0 Then
          For Each row As DataRow In dt.Rows
              ' MsgBox(sql)
              Dim p As New Panel()
              If row(2).ToString.Trim(&quot; &quot;) = &quot;panel&quot; Then
                  '  MsgBox(dt.Rows(0)(0))
                  Dim pnl As New Panel()
                  pnl.ID = &quot;pnl&quot; &amp; row(0)
                  pnl.GroupingText = row(9)
                  p = pnl
                  load_form(p, row(0))
              End If
              If row(2).ToString.Trim() = &quot;textbox&quot; Then
                  Dim t As New TextBox With {
                      .CssClass = &quot;form-control&quot;}
                  t.Attributes.Add(&quot;showto&quot;, &quot;true&quot;)
                  t.Attributes.Add(&quot;dbdata&quot;, &quot;true&quot;)
                  t.Attributes.Add(&quot;placeholder&quot;, &quot;Enter O2 Level...&quot;)
                  t.ID = &quot;Txt&quot; &amp; i
                  p.Controls.Add(t)
              End If
              If row(2).ToString.Trim() = &quot;checkbox&quot; Then
                  Dim cb As New CheckBox With {
                      .CssClass = &quot;cb form-control&quot;}
                  cb.Attributes.Add(&quot;showto&quot;, &quot;All&quot;)
                  cb.Attributes.Add(&quot;dbdata&quot;, &quot;true&quot;)
                  cb.Attributes.Add(&quot;title&quot;, row(&quot;title&quot;).ToString.Trim())
                  cb.Attributes.Add(&quot;altValue&quot;, row(&quot;altvalue&quot;).ToString.Trim())
                  cb.Text = row(7).ToString.Trim()
                  cb.ID = &quot;cb&quot; &amp; i
                  p.Controls.Add(cb)
              End If
              c.Controls.Add(p)
     
          Next
     End If
  End Sub
  </code></pre>