Home All Groups Group Topic Archive Search About

How to read value of a dynamically created radiobuttonlist control?

Author
13 Feb 2006 12:27 PM
Jarkko Turpeinen
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.

Author
13 Feb 2006 12:38 PM
Christopher Reed
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."

Show quoteHide quote
"Jarkko Turpeinen" <jar***@jarkko.com> wrote in message
news:e3c9hgJMGHA.3984@TK2MSFTNGP14.phx.gbl...
> 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.
>
>
Author
14 Feb 2006 6:39 AM
Jarkko Turpeinen
Here 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" <carttu@nospam.nospam> kirjoitti
viestissä:OgeQ2qJMGHA.3***@TK2MSFTNGP09.phx.gbl...
Show quoteHide quote
> 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" <jar***@jarkko.com> wrote in message
> news:e3c9hgJMGHA.3984@TK2MSFTNGP14.phx.gbl...
>> 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.
>>
>>
>
>
Author
14 Feb 2006 9:10 PM
Teemu Keiski
Morjens,

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



Show quoteHide quote
"Jarkko Turpeinen" <jar***@jarkko.com> wrote in message
news:%23F5kDDTMGHA.2300@TK2MSFTNGP15.phx.gbl...
> Here 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" <carttu@nospam.nospam> kirjoitti
> viestissä:OgeQ2qJMGHA.3***@TK2MSFTNGP09.phx.gbl...
>> 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" <jar***@jarkko.com> wrote in message
>> news:e3c9hgJMGHA.3984@TK2MSFTNGP14.phx.gbl...
>>> 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.
>>>
>>>
>>
>>
>
>