Dynamically Creating RadioButtonList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tapi

    Dynamically Creating RadioButtonList

    Hello there,



    I am new to ASP .NET and am tryimg to create a RadioButtonList dynamically.
    My code below gives an "Index was out of range" error.

    Where am I going wrong?





    Sub CreateRadioButt ons(ByVal dr)

    ' Instantiate RadioButtonList

    Dim DynamicRadioBut tonList As New RadioButtonList

    Dim t As Integer

    t = 0

    Do While dr.Read()

    DynamicRadioBut tonList.Items(t ).Text = dr("PRO_en")

    t = t + 1

    Loop

    'Add radio button

    PlaceHolderGrou p.Controls.Add( DynamicRadioBut tonList)

    End Sub



    Thank you for your help


  • Tapi

    #2
    Re: Dynamically Creating RadioButtonList

    I have managed to solve my problem but that lead to asecond problem.

    My new code is as follows:

    Sub CreateRadioButt ons(ByVal dr)

    ' Instantiate RadioButtonList

    Dim RadioButtonList Profiles As New RadioButtonList

    'Add radiobuttonlist into a placeholder

    PlaceHolderProf iles.Controls.A dd(RadioButtonL istProfiles)


    Do While dr.Read()

    RadioButtonList Profiles.Items. Add(New ListItem(dr("PR O_en")))

    Loop

    End Sub



    However I would now like to change the code on the HTML page so it can
    access the radiobuttonlist via the control. Before, the code on the HTML
    page was as foolows:

    Sub chkLayout_Check edChanged(sende r As Object, e As EventArgs)

    If chkLayout.Check ed = True Then RadioButtonList Profiles.Repeat Layout =
    RepeatLayout.Ta ble

    Else RadioButtonList Profiles.Repeat Layout = RepeatLayout.Fl ow

    End If

    End Sub

    and it worked very well. How do I now reference the propetries of the
    RadioButtonList through the PlaceHolderProf iles?



    Thank you



    "Tapi" <t.jongwe@free. fr> wrote in message
    news:%23YjkQCkz FHA.3188@TK2MSF TNGP14.phx.gbl. ..
    [color=blue]
    > Hello there,
    >
    >
    >
    > I am new to ASP .NET and am tryimg to create a RadioButtonList[/color]
    dynamically.[color=blue]
    > My code below gives an "Index was out of range" error.
    >
    > Where am I going wrong?
    >
    >
    >
    >
    >
    > Sub CreateRadioButt ons(ByVal dr)
    >
    > ' Instantiate RadioButtonList
    >
    > Dim DynamicRadioBut tonList As New RadioButtonList
    >
    > Dim t As Integer
    >
    > t = 0
    >
    > Do While dr.Read()
    >
    > DynamicRadioBut tonList.Items(t ).Text = dr("PRO_en")
    >
    > t = t + 1
    >
    > Loop
    >
    > 'Add radio button
    >
    > PlaceHolderGrou p.Controls.Add( DynamicRadioBut tonList)
    >
    > End Sub
    >
    >
    >
    > Thank you for your help
    >
    >[/color]


    Comment

    • Bob Barrows [MVP]

      #3
      Re: Dynamically Creating RadioButtonList

      Tapi wrote:[color=blue]
      > Hello there,
      >
      >
      >
      > I am new to ASP .NET and am tryimg to create a RadioButtonList
      > dynamically. My code below gives an "Index was out of range" error.
      >[/color]

      There was no way for you to know it, but this is a classic asp newsgroup.
      While you may be lucky enough to find a dotnet-knowledgeable person here who
      can answer your question, you can eliminate the luck factor by posting your
      question to a group where those dotnet-knowledgeable people hang out. I
      suggest microsoft.publi c.dotnet.framew ork.aspnet.

      --
      Microsoft MVP - ASP/ASP.NET
      Please reply to the newsgroup. This email account is my spam trap so I
      don't check it very often. If you must reply off-line, then remove the
      "NO SPAM"


      Comment

      • Bob Barrows [MVP]

        #4
        Re: Dynamically Creating RadioButtonList

        Tapi wrote:[color=blue]
        > I have managed to solve my problem but that lead to asecond problem.
        >
        > My new code is as follows:
        >
        > Sub CreateRadioButt ons(ByVal dr)[/color]

        Why aren't you explicitly declaring the datatype of dr? I assume dr isn't
        simply an Object, is it? I assume it's a datareader so you should declare it
        as such. You should turn on Option Strict to prevent you from making this
        mistake.


        <snip>[color=blue]
        >
        >
        > However I would now like to change the code on the HTML page so it can
        > access the radiobuttonlist via the control. Before, the code on the
        > HTML page was as foolows:
        >
        > Sub chkLayout_Check edChanged(sende r As Object, e As EventArgs)
        >
        > If chkLayout.Check ed = True Then RadioButtonList Profiles.Repeat Layout
        > = RepeatLayout.Ta ble
        >
        > Else RadioButtonList Profiles.Repeat Layout = RepeatLayout.Fl ow
        >
        > End If
        >
        > End Sub[/color]

        This isn't html code ...? This is server-side VB.Net code.

        [color=blue]
        >
        > and it worked very well. How do I now reference the propetries of the
        > RadioButtonList through the PlaceHolderProf iles?
        >
        >[/color]
        I had never heard of the Placeholder class so I looked it up in online help.
        Everything seems to be there. You should probably go read it and, if it
        doesn't answer your question, try the aspnet group.

        Bob Barrows
        --
        Microsoft MVP - ASP/ASP.NET
        Please reply to the newsgroup. This email account is my spam trap so I
        don't check it very often. If you must reply off-line, then remove the
        "NO SPAM"


        Comment

        Working...