Public Function getStarElements() As RadioButtonList
Dim radioBtnList As RadioButtonList = New RadioButtonList
radioBtnList.Items.Add(getStarListItem("test-3B-1" & listNumber, "value1", "hover-star", "value2"))
radioBtnList.Items.Add(getStarListItem("test-3B-1" & listNumber, "value2", "hover-star", "value2"))
radioBtnList.Items.Add(getStarListItem("test-3B-1" & listNumber, "value3", "hover-star", "value2"))
Response.Write("here: " & radioBtnList.SelectedValue & "<br />")
Return radioBtnList
End Function
Public Function getStarListItem(ByVal name As String, ByVal scaleRating As String, ByVal className As String, ByVal compareRating As String) As ListItem
Dim listItem As ListItem = New ListItem
listItem.Attributes.Add("name", name)
listItem.Attributes.Add("type", "radio")
listItem.Attributes.Add("value", scaleRating)
listItem.Attributes.Add("class", className)
listItem.Attributes.Add("title", scaleRating)
If compareRating = scaleRating Then
listItem.Selected = True
Response.Write(compareRating & " seclected <br />")
End If
Return listItem
End Function
Why when i do a response.write on the selected value of the radioButtonlist it does not return any value?
Please debug and see whether the above condition is evaluating to true or not. It seems like your listItem.Selected=True line never getting chance to run. I'm not a VB guy so I assume your code is correct otherwise.
Which item do you want to return in your radioBtnList.SelectedValue? You could set the radioBtnList.SelectedIndex
before you get the value. If you need the first values, set radioBtnList.SelectedIndex = 0.
rezelute
Member
108 Points
93 Posts
Getting the selectedValue of dynamically created RadioButtonList
Nov 30, 2012 04:14 PM|LINK
Hi,
I am creating a radioButtonList as follows:
Public Function getStarElements() As RadioButtonList Dim radioBtnList As RadioButtonList = New RadioButtonList radioBtnList.Items.Add(getStarListItem("test-3B-1" & listNumber, "value1", "hover-star", "value2")) radioBtnList.Items.Add(getStarListItem("test-3B-1" & listNumber, "value2", "hover-star", "value2")) radioBtnList.Items.Add(getStarListItem("test-3B-1" & listNumber, "value3", "hover-star", "value2")) Response.Write("here: " & radioBtnList.SelectedValue & "<br />") Return radioBtnList End Function Public Function getStarListItem(ByVal name As String, ByVal scaleRating As String, ByVal className As String, ByVal compareRating As String) As ListItem Dim listItem As ListItem = New ListItem listItem.Attributes.Add("name", name) listItem.Attributes.Add("type", "radio") listItem.Attributes.Add("value", scaleRating) listItem.Attributes.Add("class", className) listItem.Attributes.Add("title", scaleRating) If compareRating = scaleRating Then listItem.Selected = True Response.Write(compareRating & " seclected <br />") End If Return listItem End FunctionWhy when i do a response.write on the selected value of the radioButtonlist it does not return any value?
Thanks :)
Vipindas
Contributor
5514 Points
810 Posts
Re: Getting the selectedValue of dynamically created RadioButtonList
Nov 30, 2012 04:59 PM|LINK
After PostBack dynamic controls are disappear from the form. So you've to recreate the dynamic control On each postback
Refer this
http://makhaai.blogspot.in/2010/07/server-side-code-code-snippet-for.html
rezelute
Member
108 Points
93 Posts
Re: Getting the selectedValue of dynamically created RadioButtonList
Nov 30, 2012 10:27 PM|LINK
But i havent posted back anything, i am trying to access the selected value as soon as they are created...
Ruchira
All-Star
43056 Points
7040 Posts
MVP
Re: Getting the selectedValue of dynamically created RadioButtonList
Dec 01, 2012 03:25 PM|LINK
Hello,
Please debug and see whether the above condition is evaluating to true or not. It seems like your listItem.Selected=True line never getting chance to run. I'm not a VB guy so I assume your code is correct otherwise.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.rezelute
Member
108 Points
93 Posts
Re: Getting the selectedValue of dynamically created RadioButtonList
Dec 01, 2012 07:38 PM|LINK
Hi, I can confirm that the code definitely does execute because it is response writing to the page when it enters that if statement.
Chen Yu - MS...
All-Star
21600 Points
2493 Posts
Microsoft
Re: Getting the selectedValue of dynamically created RadioButtonList
Dec 06, 2012 08:26 AM|LINK
Hi,
Which item do you want to return in your radioBtnList.SelectedValue? You could set the radioBtnList.SelectedIndex before you get the value. If you need the first values, set radioBtnList.SelectedIndex = 0.
Reference on : http://stackoverflow.com/questions/5662113/set-radiobuttonlist-selected-from-codebehind
Thanks.
Feedback to us
Develop and promote your apps in Windows Store
rezelute
Member
108 Points
93 Posts
Re: Getting the selectedValue of dynamically created RadioButtonList
Dec 06, 2012 05:12 PM|LINK
Thanks, this is what i have had to use to get around it and then translating the index to the value afterwards.
Thanks for the confirmation though :)