get selected value of radiobuttonlist dynamically

Posted on 2011-02-12
Last Modified: 2012-06-21
i have created a radiobuttonlist dynamically and now i want to get the selected value of the list.
 here is how i created radiobuttonlist dynamically
row = New HtmlTableRow()
                    cell = New HtmlTableCell
                    Dim rbl As New RadioButtonList
                    rbl.ID = "rblID"
                    rbl.Items.Add(New ListItem("Yes", "1"))
                    rbl.Items.Add(New ListItem("No", "2"))

Open in new window

here is how i tried to get selected value. but an error occuers
Dim c As RadioButtonList = DirectCast(FindControl("rblID"), RadioButtonList)
TextBox1.Text = c.SelectedItem.Text

Open in new window

error is: Object reference not set to an instance of an object. on line 2 of second coding
all these codings are done codebehind file.
Question by:issay
LVL 10

Expert Comment

ID: 34882399
Controls that are dynamically created, no longer exist on the page during postback, because a page only auto-creates the controls that are assigned to it at design time.  However, the values of dynamic controls are saved in the ViewState, so to access the value simply recreate the control with the exact same name, and you will be able to access it's value.

I believe these lines of code would do the trick:

Dim rbl As New RadioButtonList
rbl.ID = "rblID"
rbl.Items.Add(New ListItem("Yes", "1"))
rbl.Items.Add(New ListItem("No", "2"))

Maybe even just this:

Dim rbl As New RadioButtonList
rbl.ID = "rblID"

An then you should be able to set your textbox value like this:

TextBox1.Text = rbl.SelectedItem.Text
LVL 20

Expert Comment

ID: 34882497
Have you written the code to create the radiobuttonlist in the Page Load event?

Make sure you check the IsPostBack Condition.


Author Comment

ID: 34885390
the way you told also not working. same error occurs.

i have created radiobuttonlist in page load event. i have tried both the ways. but still same error
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Accepted Solution

P1ST0LPETE earned 500 total points
ID: 34885645
Sorry, was trying to tell you how to do it from memory.  Anyway, below is a working version:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Panel ID="Panel1" runat="server" />
        <br />
        <asp:TextBox ID="Textbox1" runat="server" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />

Open in new window

Code Behind:
Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim c As RadioButtonList = DirectCast(Me.FindControl("rblID"), RadioButtonList)
        Textbox1.Text = c.SelectedItem.ToString()
    End Sub

    Private Sub CreateControls()
        Dim rbl As New RadioButtonList
        rbl.ID = "rblID"
        rbl.Items.Add(New ListItem("Yes", "1"))
        rbl.Items.Add(New ListItem("No", "2"))
        rbl.SelectedIndex = 0
    End Sub

End Class

Open in new window


Author Comment

ID: 34949440
correct anser for my question is

Dim radio1 As RadioButtonList = DirectCast(newtables.FindControl("rblID"), RadioButtonList)
TextBox1.Text = radio1.SelectedItem.Text

i figure it out by testing several codes
by the ways informaniac:and P1ST0LPETE:  idea was helpfull thanks
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35829850
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

713 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