473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Retrieving the chosen value / selected index from a RadioButtonList

3
Hi, how do I retrieve the chosen value from a RadioButtonList? I have a RadioButtonList that is populated dynamically with a "Next" button. Ie, I have a list of questions each with 2-3 answer options. When the "Next" button is pressed, it fetches the next question from the DB and the corresponding set of answers (only one of which is true). Then when the user chooses his answer / radio button from the list, he uses the same "Next" button to submit his selection to the server. That means that the next time the page loads, the question is different and the RadioButtonList gets populated with a new set of answers.

My problem is that the SelectedIndexChanged method of the RaioButtonList is not being executed. How do I capture the user's answer selection?

Expand|Select|Wrap|Line Numbers
  1. Protected Sub Next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles [Next].Click
  2.  
  3.        'this is always false: 
  4.             If list.SelectedIndex > -1 Then
  5.                 label.Text = "You chose: " & list.SelectedItem.Text
  6.             End If
  7.  
  8.         If Not Page.IsCallback Then
  9.             If Not Session("count") Is Nothing Then
  10.                 count = Session("count")
  11.                 situationIds = Session("sit_ids")
  12.                 list.AutoPostBack = False
  13.                 list.ID = "list"
  14.                 AddHandler list.SelectedIndexChanged, AddressOf list_SelectedIndexChanged
  15.             Else : count = 0
  16.             End If
  17.             If count < situationIds.Count Then
  18.                 sit_id = situationIds.Item(count)
  19.                 count = count + 1
  20.                 Session("count") = count
  21.                 'get the associated answers
  22.                 dataReader = dbu.getAnswersBySituationId(sit_id)
  23.  
  24.                 While dataReader.Read
  25.                     Dim id As Integer = dataReader.Item("id") 'works
  26.                     Dim answer As String = dataReader.Item("text") 'works
  27.                     ' Add the items
  28.                     list.Items.Add(New ListItem(answer, answer))
  29.                     'list.Items.Add(New ListItem("Two", "2"))
  30.                 End While
  31.  
  32.                 list.DataBind()
  33.                 holder.Controls.Add(list)
  34.  
  35.             End If
  36.         End If
  37.     End Sub
  38.  
  39.  
  40.     'this method never gets hit:
  41.     Protected Sub list_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles list.SelectedIndexChanged
  42.         Dim l As RadioButtonList = DirectCast(FindControl("list"), RadioButtonList)
  43.         label.Text = l.SelectedItem.Text
  44.     End Sub
Thanks beforehand!
Vahagn
Dec 27 '07 #1
2 2683
nateraaaa
663 Expert 512MB
Is your RadioButtonList AutoPostBack property set to true? If not the sequence of your page when you click the Next button will be to hit your page_load event then your next button click event. If you set the RadioButtonList to AutoPostBack = true you should be able to retrieve the selected value.

Let us know if this works.

Nathan
Dec 27 '07 #2
Vahagn
3
Hi Nathan - it is set to TRUE, but it is done separately in Page_Init (Because I moved it there from the "Next_Clicked" method while I was trying to get it to work):

Expand|Select|Wrap|Line Numbers
  1.   Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  2.         list.AutoPostBack = True
  3.         list.ID = "list"
  4.         AddHandler list.SelectedIndexChanged, AddressOf list_SelectedIndexChanged
  5.         Dim l As RadioButtonList = DirectCast(FindControl("list"), RadioButtonList)
  6.         'label.Text = l.SelectedItem.Text 'this gives a null pointer exception
  7.     End Sub

Is your RadioButtonList AutoPostBack property set to true? If not the sequence of your page when you click the Next button will be to hit your page_load event then your next button click event. If you set the RadioButtonList to AutoPostBack = true you should be able to retrieve the selected value.

Let us know if this works.

Nathan
Dec 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: asd | last post by:
I need to find the value/index of the previously selected item of a select list. That is, when the user selects an item from the list and a certain condition elsewhere in the form is not met, I...
3
by: juststarter | last post by:
Hello, I have an aspx file where i've put a placeholder element. On load i create dynamically a table which contains a checkbox and a radiobuttonlist in each tablerow . The radiobuttonlist...
2
by: mg | last post by:
After a selection is made in the following RadioButtonList <asp:RadioButtonList id="RadioButtonList1" runat="server"> <asp:ListItem Value="Y"></asp:ListItem> <asp:ListItem...
4
by: Emil | last post by:
Can somebody tell me what would be the syntax for having an if statement and setting the selected index of a radiobuttonlist? This is my first project using ASP.net and I use C#. I have a repeater...
2
by: Serg | last post by:
Hello, I need to set the selection in the RadioButtonList according to the value of SelectedValue in the underlying dataset - please suggest. The code is below. Thank you. <asp:TemplateColumn>...
2
by: Alphonse Giambrone | last post by:
Hi all, I have two radiobuttonlist controls on a page. When a user checks 'No' for rblDeleted, I want to automatically set rblSendCard to 'No' and disable it. The javascript function I wrote to...
5
by: svein.erik.storkas | last post by:
How can i get the index of the button that is selected in a radiobuttonlist? I have a radiobuttonlist that contains 5 buttons, and i do a switch on what index that is selected, like this: ...
2
by: Michael Bohman | last post by:
Hi, i have a small problem with assigning a database value to a RadioButtonList control. On my form i have 3 user admin=1, premium=2 and basic=3, theese values is stored in an access database in a...
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.