Create control at runtime

  • Thread starter Thread starter Tom Jackson
  • Start date Start date
T

Tom Jackson

Can anyone please point me in the right direction. I want to create some
simple controls (radio buttons) at runtime. The problem is that I don't know
how many radio buttons I need or where they go until I read the database at
runtime.

Thanks
 
Tom:
here's a fictious example:

Dim options As New RadioButtonList
For Each row As DataRow In ds.Tables(0).Rows
Dim item As New ListItem
item.Text = CStr(row("Name"))
item.Value = CStr(row("Id"))
options.Items.Add(item)
Next
Dim parent As String = CStr(ds.Tables(1).Rows(0)("Parent"))
Select Case parent
Case "sideNav"
columnSidenav.Controls.Add(options)
Case "footer"
footerCoontainer.Controls.add(options)
Case Else
paceholderDefault.Controls.add(options)
End Select

98% chance you can get rid of the entire for each loop and replace it with a
databind expression:

Dim options As New RadioButtonList
options.DataSource = ds.Tables(0)
options.DataTextField = "name"
options.DataValueField = "id"
options.DataBind()

the select case parent was just to show how you can add the radiobutton to
different containers at runtime..

Karl
 
Karl,

So fast!! I thought it would be more difficult! .Controls.Add was the key.

Thank you,

Tom
 
Back
Top