Solved

get selected value of radiobuttonlist dynamically

Posted on 2011-02-12
8
613 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 500 total points
Comment Utility
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
Comment Utility
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]
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP.NET C# load a .js file and process some data in code behind 5 33
crm development 2 34
Hovering effect 9 28
Hidden Field Value 10 33
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now