• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

Get RadioButtonList selection in Asp.Net 4.0

Hi,
I have a RadioButtonList control declared in markup as shown in my code snippet below. When the page loads it is possible that neither value is selected. In my code behind page I want to use its SelectedIndexChanged event to get its value after a user has selected either pass or fail. At present I've declare a page-wide variable s as string and set its initial value = test, then in the SelectedIndexChanged event I've written the code also shown in the snippet below, the value is only ever "test" because the value is set when the page loads and the changed value isn't stored across a postback.
Can anyone tell me how I can get the value pass or fail selected by the user?
Thanks,
Louise
'markup
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
    <asp:ListItem>Pass</asp:ListItem>
    <asp:ListItem>Fail</asp:ListItem>
</asp:RadioButtonList>

'SelectedIndexChanged event in code behind
If Page.IsPostBack Then

                If Me.RadioButtonList1.SelectedValue.ToString = "Pass" Then
                    Me.test = "Pass"
                End If

                Response.Write(Me.test)

            End If

Open in new window

'markup
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
    <asp:ListItem>Pass</asp:ListItem>
    <asp:ListItem>Fail</asp:ListItem>
</asp:RadioButtonList>

'SelectedIndexChanged event in code behind
If Page.IsPostBack Then

                If Me.RadioButtonList1.SelectedValue.ToString = "Pass" Then
                    Me.test = "Pass"
                End If

                Response.Write(Me.test)

            End If

Open in new window

0
louise001
Asked:
louise001
  • 3
  • 3
  • 2
1 Solution
 
dschauhan82Commented:
Solution1:
At times, there will be need to get the selected or checked RadioButtonList value from javascript to do some validations on client side.

The following javascript will help us to do that.  

<script language="javascript" type="text/javascript">      

        function GetRDBValue()

        {

            var radio = document.getElementsByName('rdbGender');

            for (var i = 0; i < radio.length; i++)

            {

                if (radio[i].checked)

                {

                    alert(radio[i].value);

                 }

            }

        }

</script>

 

<asp:RadioButtonList ID="rdbGender" runat="server">

            <asp:ListItem Text="Male" Value="1"></asp:ListItem>

            <asp:ListItem Text="Female" Value="2"></asp:ListItem>            

</asp:RadioButtonList

0
 
louise001Author Commented:
Hi,
Thanks, you're both pointing me to javascript. This is a basic question, but how do I get the script to run and pass a value to a variable in my code behind (the value will then be stored in a database)?
Thanks,
Louise
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
GeoffSuttonCommented:
If you are setting the value of the radio button list on page_load, the just make sure you're setting it inside of a If Not Page.IsPostback block.  This will prevent it from automatically being set.  
To use javascript, I have always cheated by adding a hidden field to the page.  Then in the click event (attribute OnClick="setField('pass')"; for the pass button) and javascript code
function setField(value){
 document.GetElementById('<%=hiddenField1.ClientID%>').value=value;
}
and in the selected index changed event you can retrieve the value in the hidden field.  But there should be no need to take this approach as the radio button list is designed to handle this type of value posting.  You just need to make sure, as I said, that the value is only set when it is not a postback.
HTH,
Geoff
0
 
louise001Author Commented:
Hi Geoff,
Thanks for replying. In my code behind page I've coded the RadioButtonList's SelectedIndexChanged event and placed an If Not Page.IsPostBack block in the code which adds the RadioButtonList to a tablecell (code is attached). This works as long as I don't change the selection for a global value named job_applicant, if I do change the value and then refresh the page, even if the Case Is = 2 is true the RadioButtonList isn't added to the cell I assume because a refresh counts as a postback. If I change the selection for job_applicant and also browse to another page then return to the page which runs all this code, the RadioButtonList is added to the cell.
So I added the line response.redirect(http://mysite/default.aspx) to the get_job_applicant routine and that seems to do the trick - do you think that's a stable workaround?
Also thanks for the hidden field tip.
Kind regards,
Louise

Public Sub MySub()
Dim dt As New System.Web.UI.WebControls.Table
Dim t as New DataTable = 'get data from a database
For i as Integer = 0 to t.rows.count -1
'set up the table including creating a tablecell named myCell
'this is the relevant code:
Select Case t.Rows(i)("field").ToString

Case Is = 1
'do something
Case Is = 2
Dim rbl As New RadioButtonList
If Not Page.IsPostBack Then
myCell.Controls.Add(Me.rbl)
Me.rbl.Visible = True
End If
End Select
Next

Open in new window

0
 
GeoffSuttonCommented:
I see what you are doing now - You are adding the radio button dynamically :)  In order for the page to retain the values of dynamic controls, they have to be added in the init() function:
Public override OnInit(e as eventargs)
  if not page.IsPostback Then
     Dim rbl As New RadioButtonList
     rbl.id="my_rbl"
     If Not Page.IsPostBack Then
     myCell.Controls.Add(Me.rbl)
     Me.rbl.Visible = False
  end if
Then in your mysub: Case IS=2 you would use
     (Page.FindControl("my_rbl") as RadioButtonList).visible=True
to find and set the control to be seen.  The reason is that onless it's in the init, the control is destroyed every time you post the page back:  This happens on any refresh, any postback, any update.  That is why you're having the troubles you are.
Please remember that it has been a long time since I've seriously used VB.NET - I started with C# several years ago and never looked back, so the code I've posted will likely have inconsistancies - treat it as pseudo code :)  I suspect, based on what you've said, that it will solve your problems.
Geoff
 
 
0
 
louise001Author Commented:
Hi Geoff,

Thanks, this is really helpful and much appreciated.

Kind regards,

Louise
0
 
GeoffSuttonCommented:
Glad I could help you out :)  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now