Solved

Get RadioButtonList selection in Asp.Net 4.0

Posted on 2010-08-31
8
662 Views
Last Modified: 2012-05-10
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
Comment
Question by:louise001
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:dschauhan82
ID: 33573665
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
 
LVL 4

Expert Comment

by:dschauhan82
ID: 33573668
0
 

Author Comment

by:louise001
ID: 33574748
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 10

Expert Comment

by:GeoffSutton
ID: 33576245
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
 

Author Comment

by:louise001
ID: 33577014
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
 
LVL 10

Accepted Solution

by:
GeoffSutton earned 500 total points
ID: 33578964
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
 

Author Closing Comment

by:louise001
ID: 33580950
Hi Geoff,

Thanks, this is really helpful and much appreciated.

Kind regards,

Louise
0
 
LVL 10

Expert Comment

by:GeoffSutton
ID: 33581005
Glad I could help you out :)  
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question