none
Can't add handler to dynamically added radio buttons RRS feed

  • Question

  • Using VS 2013, I have the following code in a simple webform page:

    Using RDR = Com3.ExecuteReader()
                        If RDR.HasRows Then
                            Do While RDR.Read()
                                Dim rbtn As New RadioButton
                                With rbtn
                                    rbtn.GroupName = "aaaaa"
                                    rbtn.ID = "bbbbb" & i
                                    rbtn.Text = Trim(RDR.Item("ccccc"))
                                    rbtn.Checked = False
                                    
                                    rbtn.Attributes.Add("value", i)
                                    AddHandler rtbn.CheckedChanged, AddressOf rbtn_CheckedChanged
                                End With
                                Panel1.Controls.Add(rbtn)
                                
                                Panel1.Controls.Add(New LiteralControl("<br />"))
                                Panel1.Controls.Add(New LiteralControl("<br />"))
                                i += 1
                            Loop
                            Session("ddddd") = (i - 1).ToString
                        End If
                    End Using

    Have done many variations of this code over the last week, but am still getting the error message at the AddHandler line:

     'rtbn' is not declared. It may be inaccessible due to its protection level.

    Anyone have an idea of what I could try to fix this? 

    Wednesday, February 6, 2019 4:43 AM

Answers

  • Hi,

    You declare a radiobutton name is rbtn,

    Dim rbtn As New RadioButton

    but below the code you use rtbn.

    AddHandler rtbn.CheckedChanged, AddressOf rbtn_CheckedChanged

    Best Rehgards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Tee-Rob Saturday, February 9, 2019 2:35 AM
    Friday, February 8, 2019 3:31 AM

All replies

  • Hi,

    rbtn 

    rtbn

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 6, 2019 6:31 AM
  • AddHandler rtbn.CheckedChanged, AddressOf rbtn_

    rtbn …… rbtn

    Wednesday, February 6, 2019 9:22 AM
  • Hello,

    How are you doing with this issue? If resolved please mark any replies that solve this issue as helpful/or as the solution please so others with the same issue know the solution.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 7, 2019 11:23 AM
    Moderator
  • Hi Alex, can you please explain this more?  All I'm seeing is you wrote the name of my radiobutton variable twice.
    Thursday, February 7, 2019 7:00 PM
  • Hi DA924x, can you please explain this more?  All I'm seeing is you wrote the name of my radiobutton variable twice.
    Thursday, February 7, 2019 7:00 PM
  • Hi,

    You declare a radiobutton name is rbtn,

    Dim rbtn As New RadioButton

    but below the code you use rtbn.

    AddHandler rtbn.CheckedChanged, AddressOf rbtn_CheckedChanged

    Best Rehgards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Tee-Rob Saturday, February 9, 2019 2:35 AM
    Friday, February 8, 2019 3:31 AM
  • OMG, can't believe I missed that in a week of looking at it.  Thanks!
    Saturday, February 9, 2019 2:35 AM