How to read value of a dynamically created radiobuttonlist control?

How to read value of a dynamically created radiobuttonlist control?

Post by Jarkko Tur » Tue, 14 Feb 2006 21:27:16


I'm developing a project that dynamically creates a set of radiobuttonlist
controls. I have a problem with reading the values of the radiobuttons. When
I'm using the following code to iterate through controls, I cannot use the
.selecteditem.value property after 'value = con5.'

Dim con5 As Control, con4 As Control
For Each con5 In con4.Controls
If TypeOf con5 Is RadioButtonList Then

name = con5.ID

value = con5.????????

End If

Next

When I'm using this code, the program gives Unable to cast object of type
'System.Web.UI.LiteralControl' to type
'System.Web.UI.WebControls.RadioButtonList' -error.
Dim radio As RadioButtonList, Con4 As Control

For Each radio In con4.Controls

name = radio.ID

value = radio.SelectedItem.value

Next

Could anyone help me and tell me what I am doing wrong? What would be the
best way to iterate through dynamically created controls inside a form, to
check if they are radiobuttonlists and then read their values?


Toni S.
 
 
 

How to read value of a dynamically created radiobuttonlist control?

Post by Christophe » Tue, 14 Feb 2006 21:38:45

Show the code that creates your dynamic RadioButtonLists. Indicate which
line is receiving the error message.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

 
 
 

How to read value of a dynamically created radiobuttonlist control?

Post by Jarkko Tur » Wed, 15 Feb 2006 15:39:43

ere is the code that creates the radiobuttonlist:

Dim typeofbuttonlist As Integer = myrow2.Item("TYPEOFBUTTONLIST")
c = New HtmlTableCell

Select Case typeofbuttonlist

Case 1

Dim name As String = "r" & kysymysnumero

Dim radio As New RadioButtonList

radio.ID = name

radio.RepeatDirection = RepeatDirection.Horizontal

radio.Items.Add(New ListItem("1", "1"))

radio.Items.Add(New ListItem("2", "2"))

radio.Items.Add(New ListItem("3", "3"))

radio.Items.Add(New ListItem("4", "4"))

radio.Items.Add(New ListItem("5", "5"))

c.Controls.Add(radio)

Case 2

Dim name As String = "r" & kysymysnumero

Dim radio As New RadioButtonList

radio.ID = name

radio.RepeatDirection = RepeatDirection.Horizontal

radio.Items.Add(New ListItem("4", "4"))

radio.Items.Add(New ListItem("5", "5"))

radio.Items.Add(New ListItem("6", "6"))

radio.Items.Add(New ListItem("7", "7"))

radio.Items.Add(New ListItem("8", "8"))

radio.Items.Add(New ListItem("9", "9"))

radio.Items.Add(New ListItem("10", "10"))

c.Controls.Add(radio)

Case 3

Dim name As String = "r" & kysymysnumero

Dim radio As New RadioButtonList

radio.ID = name

radio.RepeatDirection = RepeatDirection.Horizontal

radio.Items.Add(New ListItem("x", "1"))

radio.Items.Add(New ListItem("y", "2"))

c.Controls.Add(radio)

Case 4

Dim name As String = "r" & kysymysnumero

Dim radio As New RadioButtonList

radio.ID = name

radio.RepeatDirection = RepeatDirection.Horizontal

radio.Items.Add(New ListItem("yes", "1"))

radio.Items.Add(New ListItem("no", "2"))

c.Controls.Add(radio)

Case 5 ' creates a textbox

Dim name As String = "t" & kysymysnumero

Dim txtbox As New TextBox

txtbox.ID = name

txtbox.Width = 300

txtbox.Height = 100

txtbox.MaxLength = 500

txtbox.TextMode = TextBoxMode.MultiLine

c.Controls.Add(txtbox)

End Select

r.Cells.Add(c)

-----------------------

Dim radio As RadioButtonList, Con4 As Control

For Each radio In con4.Controls <---------------
name = radio.ID
value = radio.SelectedItem.value
Next

This is the line that causes the error Unable to cast object of type
'System.Web.UI.LiteralControl' to type
'System.Web.UI.WebControls.RadioButtonList'.

There are three types of controls in the form: Literalcontrols,
Radiobuttonlists and Textboxes.

Thank you for replying to my post. I would be very happy if you could help.

Toni Sevastjanoff

"Christopher Reed" < XXXX@XXXXX.COM > kirjoitti
viestiss XXXX@XXXXX.COM ... >> Show the code that creates your dynamic RadioButtonLists. Indicate which >> line is receiving the error message. >> -- >> Christopher A. Reed >> "The oxen are slow, but the earth is patient." >> >> "Jarkko Turpeinen"<< XXXX@XXXXX.COM >> wrote in message >> news: XXXX@XXXXX.COM ... >>> I'm developing a project that dynamically creates a set of >>> radiobuttonlist controls. I have a problem with reading the values of the >>> radiobuttons. When I'm using the following code to iterate through
 
 
 

How to read value of a dynamically created radiobuttonlist control?

Post by Teemu Keis » Thu, 16 Feb 2006 06:10:13

orjens,

Something like:

Dim child As Control, Con4 As Control

For Each child In con4.Controls
If TypeOf child is RadioButtonList Then
Dim rdbl As RadioButtonlist = DirectCast(child,RadioButtonList)
name = rdbl.ID
value = rdbl.SelectedItem.value
End If
Next

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke



"Jarkko Turpeinen" < XXXX@XXXXX.COM > wrote in message
news:% XXXX@XXXXX.COM ...