?
Solved

get selected value of radiobuttonlist dynamically

Posted on 2011-02-12
8
Medium Priority
?
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 2000 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 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.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 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