Solved

Can't find Radio Button Array Values

Posted on 2011-09-02
11
392 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now