By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Retrieving the chosen value / selected index from a RadioButtonList

P: 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
Share this Question
Share on Google+
2 Replies


nateraaaa
Expert 100+
P: 663
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

P: 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

Post your reply

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