Dynamically Creating RadioButtonList

T

Tapi

Hello there,



I am new to ASP .NET and am tryimg to create a RadioButtonList dynamically.
My code below gives an "Index was out of range" error.

Where am I going wrong?





Sub CreateRadioButtons(ByVal dr)

' Instantiate RadioButtonList

Dim DynamicRadioButtonList As New RadioButtonList

Dim t As Integer

t = 0

Do While dr.Read()

DynamicRadioButtonList.Items(t).Text = dr("PRO_en")

t = t + 1

Loop

'Add radio button

PlaceHolderGroup.Controls.Add(DynamicRadioButtonList)

End Sub



Thank you for your help
 
T

Tapi

I have managed to solve my problem but that lead to asecond problem.

My new code is as follows:

Sub CreateRadioButtons(ByVal dr)

' Instantiate RadioButtonList

Dim RadioButtonListProfiles As New RadioButtonList

'Add radiobuttonlist into a placeholder

PlaceHolderProfiles.Controls.Add(RadioButtonListProfiles)


Do While dr.Read()

RadioButtonListProfiles.Items.Add(New ListItem(dr("PRO_en")))

Loop

End Sub



However I would now like to change the code on the HTML page so it can
access the radiobuttonlist via the control. Before, the code on the HTML
page was as foolows:

Sub chkLayout_CheckedChanged(sender As Object, e As EventArgs)

If chkLayout.Checked = True Then RadioButtonListProfiles.RepeatLayout =
RepeatLayout.Table

Else RadioButtonListProfiles.RepeatLayout = RepeatLayout.Flow

End If

End Sub

and it worked very well. How do I now reference the propetries of the
RadioButtonList through the PlaceHolderProfiles?



Thank you
 
B

Bob Barrows [MVP]

Tapi said:
Hello there,



I am new to ASP .NET and am tryimg to create a RadioButtonList
dynamically. My code below gives an "Index was out of range" error.

There was no way for you to know it, but this is a classic asp newsgroup.
While you may be lucky enough to find a dotnet-knowledgeable person here who
can answer your question, you can eliminate the luck factor by posting your
question to a group where those dotnet-knowledgeable people hang out. I
suggest microsoft.public.dotnet.framework.aspnet.
 
B

Bob Barrows [MVP]

Tapi said:
I have managed to solve my problem but that lead to asecond problem.

My new code is as follows:

Sub CreateRadioButtons(ByVal dr)

Why aren't you explicitly declaring the datatype of dr? I assume dr isn't
simply an Object, is it? I assume it's a datareader so you should declare it
as such. You should turn on Option Strict to prevent you from making this
mistake.


However I would now like to change the code on the HTML page so it can
access the radiobuttonlist via the control. Before, the code on the
HTML page was as foolows:

Sub chkLayout_CheckedChanged(sender As Object, e As EventArgs)

If chkLayout.Checked = True Then RadioButtonListProfiles.RepeatLayout
= RepeatLayout.Table

Else RadioButtonListProfiles.RepeatLayout = RepeatLayout.Flow

End If

End Sub

This isn't html code ...? This is server-side VB.Net code.

and it worked very well. How do I now reference the propetries of the
RadioButtonList through the PlaceHolderProfiles?
I had never heard of the Placeholder class so I looked it up in online help.
Everything seems to be there. You should probably go read it and, if it
doesn't answer your question, try the aspnet group.

Bob Barrows
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top