[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Creating dynamic radiobuttonlist from datatable in vb.net

Posted on 2007-07-23
Medium Priority
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

Bob Learned earned 1600 total points
ID: 19549703
Try this:

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


Assisted Solution

ggable313 earned 400 total points
ID: 19556313
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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 …
Starting up a Project

834 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