Create control at runtime

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
 
K

Karl Seguin

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
 
T

Tom Jackson

Karl,

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

Thank you,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top