Solved

get selected value of radiobuttonlist dynamically

Posted on 2011-02-12
8
617 Views
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"))
                    rbl.DataBind()
                    cell.Controls.Add(rbl)
                    row.Controls.Add(cell)
                    newtables.Controls.Add(row)

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.
0
Comment
Question by:issay
8 Comments
 
LVL 10

Expert Comment

by:P1ST0LPETE
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"))
rbl.DataBind()

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
0
 
LVL 20

Expert Comment

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

Make sure you check the IsPostBack Condition.

0
 

Author Comment

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

informaniac:
i have created radiobuttonlist in page load event. i have tried both the ways. but still same error
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 10

Accepted Solution

by:
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:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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" />
    </div>
    </form>
</body>
</html>

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
        CreateControls()
    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
        Panel1.Controls.Add(rbl)
    End Sub

End Class

Open in new window

0
 

Author Comment

by:issay
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
0
 
LVL 142

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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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