Solved

Can't find Radio Button Array Values

Posted on 2011-09-02
11
394 Views
Last Modified: 2012-05-12
I created an array of radiobuttons in vb code.  I have 8 groups of 2 radiobutons, so I will have 8 selections.  I populate the radio button text from a db connection.  I don't know how to get handle on the radio button or the user selections.  Help


Dim iCount As Integer = dbDataSet.Tables(0).Rows.Count

        Dim rbSelA(16) As RadioButton
        Dim rbSelB(16) As RadioButton


        For iLoop = 1 To 16

            rbSelA(iLoop) = New RadioButton
            rbSelB(iLoop) = New RadioButton

            With rbAway(iLoop)

                .Text = dbDataSet.Tables(0).Rows(iLoop - 1)("dbSela").ToString()
                .Checked = False
                .GroupName = "SEL" & iLoop
                .ID = "SelA" & iLoop

            End With

            With rbSelB(iLoop)

                .Text = dbDataSet.Tables(0).Rows(iLoop - 1)("dbBme").ToString()
                .Checked = False
                .GroupName = "SEL" & iLoop
                .ID = "SelB" & iLoop

            End With

            Panel1.Controls.Add(rbSelB(iLoop))

            Panel1.Controls.Add((New LiteralControl("<div></div>")))

            Panel2.Controls.Add(rbSelA(iLoop))

            Panel2.Controls.Add((New LiteralControl("<div></div>")))

        Next

Open in new window

0
Comment
Question by:NOTBOB90
  • 7
  • 3
11 Comments
 
LVL 6

Accepted Solution

by:
c1nmo earned 50 total points
ID: 36474740
I've taken your db code out for testing.  I've added button1 and label1 to pickup selected radiobuttons and display.


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim rbSelA(16) As RadioButton
        Dim rbSelB(16) As RadioButton


        For iLoop = 1 To 8

            rbSelA(iLoop) = New RadioButton
            rbSelB(iLoop) = New RadioButton

            With rbSelA(iLoop)

                .Text = "AR" & iLoop
                .Checked = False
                .GroupName = "SEL" & iLoop
                .ID = "SelA" & iLoop

            End With

            With rbSelB(iLoop)

                .Text = "BR" & iLoop
                .Checked = False
                .GroupName = "SEL" & iLoop
                .ID = "SelB" & iLoop

            End With

            Panel1.Controls.Add(rbSelB(iLoop))

            Panel1.Controls.Add((New LiteralControl("<div></div>")))

            Panel2.Controls.Add(rbSelA(iLoop))

            Panel2.Controls.Add((New LiteralControl("<div></div>")))

        Next

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        For iLoop = 1 To 8

            Dim a As RadioButton
            Dim b As RadioButton

            a = Panel1.FindControl("SelA" & iLoop)
            b = Panel1.FindControl("SelB" & iLoop)

            If a.Checked Then
                Label1.Text = Label1.Text & "SelA" & iLoop
            End If
            If b.Checked Then
                Label1.Text = Label1.Text & "SelB" & iLoop
            End If
        Next

    End Sub
0
 

Author Comment

by:NOTBOB90
ID: 36474829
On the if a.checked then line I get this error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
0
 
LVL 7

Expert Comment

by:gopaltayde
ID: 36475178
Do you want event for radio buttons? If yes then try code like following

Button b = new Button();
            b.Text = name;
            b.ID = id;
            b.Click += new EventHandler(Button_Click);

You can easily change the button to RadioButton and event from Click to CheckedChange
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:NOTBOB90
ID: 36475228
There is no click event for a radio button.
0
 

Author Comment

by:NOTBOB90
ID: 36475347
So to be clear what I am looking for:

Here is what is on user screen that is pulled from Database (I have alread done this part)

  O Soup   O  Salad
  O  Fries  O  Toast
  O  Pepsi  O  Coke

So user would make three selection from above. Above would be radio buttons in array of 6.

After the user makes selection, I want to know the text they selected.  Because it is gettting pulled from DB, the actual words and order may change.  That is why I want the value like "Soup, Toast, Pepsi"  Getting rbSelA.checked = true is not that helpful unless I can reference exactly which of the arrays returned that.

 

     
0
 
LVL 6

Expert Comment

by:c1nmo
ID: 36475593
Are the dynamic radio buttons being recreated on the postback?

Examining the checked property is just to see if it selected, you can also read the text property which would give you the value from the db.
0
 

Author Comment

by:NOTBOB90
ID: 36475750
RIght now I have a page setup that has a button on it.  WHen you click the button it creates the radio buttons dynamically.  I added that other code to another button, but it is almost like the findcontrol is not getting the RB.  I understand your last comment about the checked property and the text, but I am not able to see any properties of the rb arrays.
0
 
LVL 6

Expert Comment

by:c1nmo
ID: 36476044
That's why I've created the dynamic radio buttons in the form load event, they need to be recreated on each postback otherwise they will not exist in the code of your other button.
0
 

Author Comment

by:NOTBOB90
ID: 36490488
I added it to the load page and I still get same error
0
 

Author Comment

by:NOTBOB90
ID: 36491592
Ohhh, I got it to work sort of...The button now finds the control.  Is there a way to create a handle for the radio button?
0
 

Author Closing Comment

by:NOTBOB90
ID: 36491602
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I build my own IDE using ASP.NET MVC? 2 45
Message not shown 5 50
Work with App store 7 53
Where can I ask about my ASP.NET MVC project? 6 37
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question