Link to home
Start Free TrialLog in
Avatar of issay
issay

asked on

get selected value of radiobuttonlist dynamically

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.
Avatar of P1ST0LPETE
P1ST0LPETE
Flag of United States of America image

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
Avatar of Obadiah Christopher
Have you written the code to create the radiobuttonlist in the Page Load event?

Make sure you check the IsPostBack Condition.

Avatar of issay
issay

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of P1ST0LPETE
P1ST0LPETE
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of issay

ASKER

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
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.