Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can't find Radio Button Array Values

Posted on 2011-09-02
11
Medium Priority
?
401 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

927 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