Creating dynamic radiobuttonlist from datatable in

Posted on 2007-07-23
Last Modified: 2012-06-27
Hi guys, I need help with creating a dynamic radiobuttonlist. There is a radiobuttonlist that sits as an empty control. I am creating a datatable from a stored procedure, and then looping through the rows adding a new radiobutton with each passthrough. This works fine, no problems. The only thing is the actual value of each radiobutton is the actual text of the question that is getting populated. Here is the current population code :      

Dim i As Integer
        For i = 0 To dt.Rows.Count - 1
            Dim dr As DataRow = dt.Rows(i)

Now what I need to do differently is this. I need to generate this radiobuittonlist dynamically, making the text of the radio button choice this : dr.Item("LongQuestionText"). I also need to make the value of the radiobutton this : dr.Item("PortalOfferID"). How can I do this?

To clarify if the "LongQuestionText" is "What is my name?" Then I get a radio button that looks like this in code : <input id="rdlQuestions_0" type="radio" name="rdlQuestions" value="What is my name?"/><label for="rdlQuestions_0">IWhat is my name?</label>. I need to somehow tie that PortalOfferID to the radiobutton so that I can write code against it.
Question by:nuvium
    LVL 96

    Accepted Solution

    Try this:

        For Each dr As DataRow In dt.Rows
          rdlQuestions.Items.Add(New ListItem(dr("LongQuestionText").ToString(), dr("PortalOfferID")))
        Next dr

    LVL 1

    Assisted Solution

    You can also just bind the data using this:

    rdlQuestions.Datasource = dt

    Then on your control, specify which field is the value and which is the text:
    <asp:RadioButtonList ID="rdlQuestions" runat="server" DataTextField="LongQuestionText" DataValueField="PortalOfferID"></asp:RadioButtonList>

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This is about my first experience with programming Arduino.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now