Solved

Get RadioButtonList selection in Asp.Net 4.0

Posted on 2010-08-31
8
651 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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:louise001
Comment Utility
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
 
LVL 10

Expert Comment

by:GeoffSutton
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:louise001
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Geoff,

Thanks, this is really helpful and much appreciated.

Kind regards,

Louise
0
 
LVL 10

Expert Comment

by:GeoffSutton
Comment Utility
Glad I could help you out :)  
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now