Reference programmatically created radiobuttonlist selection

asp.net , vb , radiobuttonlist United States
  • 7 years ago

    I don't know until the page is created how many radiobuttonlists I will need. So, I create them and store the number in a hidden label. But, when I want to get the selection from the radiobuttonlist on postback, I keep getting a object reference error. I've tried finding the control without a findControl, with a findControl and with a master page content reference and findControl. No luck. Any help appreciated...

                    Dim myConStr As String = ""
                    If lblSidCount.Text <> "0" Then
                        Dim x As Integer = 1, y As Integer = Convert.ToInt32(lblSidCount.Text)
                        Do While x <= y
                            Dim myContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
                            Dim radConStr As New RadioButtonList
                            radConStr = CType(myContent.FindControl("rad" & x.ToString), RadioButtonList)
                            myConStr &= radConStr.SelectedItem.Text & "~"
                            x = x + 1
                        Loop
                        myConStr = Left(myConStr, Len(myConStr) - 1)
                    End If
    
  • 7 years ago

    This seems to work - am I crazy for doing this?

        Private radCon As RadioButtonList()
        Protected Overrides Sub OnInit(ByVal e As EventArgs)
            MyBase.OnInit(e)
            If (Request("str") = 1) Then
                Dim myList As dsSql = New dsSql()   ''''instantiate the function to get dataset
                Dim ds As New Data.DataSet
                ds = myList.dsConSessionTimes(Request("eid"))
                If ds.Tables("conSessionTimes").Rows.Count > 0 Then
                    radCon = New RadioButtonList(ds.Tables("conSessionTimes").Rows.Count - 1) {}
                    Dim i As Integer = 0
                        Dim rad As RadioButtonList = New RadioButtonList()
                        rad.ID = "rad" + i.ToString()
                        rad.DataSource = ds.Tables("conSessions")
                        rad.DataTextField = ds.Tables("conSessions").Columns("session_name").ToString
                        rad.DataBind()
                        rad.SelectedIndex = 0
                        phConcurrent.Controls.Add(rad)
                        radCon(i) = rad
                    For Each j As Data.DataRow In ds.Tables("conSessionTimes").Rows
                        Dim rad As RadioButtonList = New RadioButtonList()
                        rad.ID = "rad" + i.ToString()
                        rad.DataSource = ds.Tables("conSessions")
                        rad.DataTextField = ds.Tables("conSessions").Columns("session_name").ToString
                        rad.DataBind()
                        rad.SelectedIndex = 0
                        phConcurrent.Controls.Add(rad)
                        radCon(i) = rad
                        i = i + 1
                    Next
                End If
            End If
        End Sub
    

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon