[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

get selected value of radiobuttonlist dynamically

Posted on 2011-02-12
8
Medium Priority
?
636 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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