Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can't find Radio Button Array Values

Posted on 2011-09-02
11
Medium Priority
?
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
11 Comments
 
LVL 6

Accepted Solution

by:
c1nmo earned 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

721 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