Solved

Can't find Radio Button Array Values

Posted on 2011-09-02
11
399 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 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

617 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