Solved

Get RadioButtonList selection in Asp.Net 4.0

Posted on 2010-08-31
8
678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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