Solved

ASP.NET, need JavaScript to determine which radio button was selected and pass to Code Behind

Posted on 2014-09-09
9
865 Views
Last Modified: 2014-09-24
I have an asp.net application has radio buttons that are dynamically built.  When the user clicks on a link button ("Next") I need to know which radio button the user selected.

As a simple example
ASP.NET
<asp:RadioButton ID="rbCorvet" runat="server" GroupName="Cars" /> 
<asp:RadioButton ID="rbCamaro" runat="server" GroupName="Cars" /> 
<asp:CheckBox ID="chkInsurance" runat="server" />
<asp:linkbutton id="lnkNext" Runat="server" onclick="lnkNext_Click">Next</asp:linkbutton>

Open in new window


C#
protected void lnkNext_Click(object sender, System.EventArgs e)
{

}

Open in new window


Thank You
0
Comment
Question by:CipherIS
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40312456
The following doesn't work?
if(rbCorvet.Checked)
{
// rbCorvet is checked
}
else if(rbCamaro.Checked)
{
// rbCamaro is checked
}
else
{
// none of both is checked
}

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 40312606
It's more complicated than what I used as an example.  The list is built dynamically.  I hard coded the values to make it easier.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40312656
It rarely makes it easier for us to help you if you don't post the environment as it exists. Posting the exact code that gives problems allows us to see where you may or may not have went wrong. It's similar to going to a mechanic, and saying, "There's something wrong with my car. I brought my friend's car because he lives closer."
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.

 
LVL 1

Author Comment

by:CipherIS
ID: 40312948
@kaufmed - I do agree with you.  The code is very large and proprietary which is the reason why I tried the approach I did.  

I do have to disagree with you in one regard that is your analogy of your friend's car.  If your friend's car had a similar issue you can use that as a place to start for troubleshooting.  "Hey my brake goes down to the floor when I brake".  Friend - "same thing happened to me and I had to get my brake fluid changed" or "i needed new rotors and pads".
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40313039
Off-topic:

If your friend's car had a similar issue you can use that as a place to start for troubleshooting.
But you're changing the condition of why I brought the car. I did not say that I brought my friend's car because it had a similar condition; I said that I brought it because it is closer (in distance).

But let's say that we do change the condition. Let's say that I did bring my friend's car because it has the same condition. What if my friend doesn't have the same kind of car as me? What if I have a Kia Rio and he has a Nissan Titan truck? Those are two completely different vehicles. How can you say with certainty that the symptoms displaying in both vehicles are caused by the same issue?

On-topic:

At which point in the page life-cycle are you creating these dynamic controls? And are you re-creating them on post-back?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 40313205
Off-topic
You brought your car because it was closer in distance which I don't think was a good analogy to the issue that is why I used the analogy of troubleshooting.  Even if you have a Kia and he has a Nissan Titan truck there are different parts which are not interchangeable but the parts still are similar in nature and work similar.  You have an engine block, pistons, piston rings, spark plugs, valve cover, timing belt/chain, power steering, etc....  

On-topic:
Post-back
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40313276
No, by "page life-cyle" I mean which event:

http://msdn.microsoft.com/en-us/library/ms178472(v=vs.85).aspx
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40313659
CipherIS,

As you said you are creating the controls dynamically, you might have recreating them on postback. You need to save the state of controls with hidden fields/view state and set it back to the controls on postback.
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 40313670
Added a hidden field to store the selected value so that you can get it on postback.
Check the below script:

 <script type="text/javascript">
        function setSelectionValue() {
            if (document.getElementById('rbCorvet').checked) {
                document.getElementById('hfSelection').value = 'rbCorvet';
            }
            else if (document.getElementById('rbCamaro').checked) {
                document.getElementById('hfSelection').value = 'rbCamaro';
            }
            var selectedControl=document.getElementById('hfSelection').value;
            alert(selectedControl);
        }
    </script>

 <asp:HiddenField ID="hfSelection" runat="server" />
                            <asp:RadioButton ID="rbCorvet" runat="server" GroupName="Cars" />
                            <asp:RadioButton ID="rbCamaro" runat="server" GroupName="Cars" />

                            <asp:CheckBox ID="chkInsurance" runat="server" />
                            <asp:LinkButton ID="lnkNext" runat="server" OnClick="lnkNext_Click" OnClientClick="setSelectionValue();">Next</asp:LinkButton>

Hope this will work for you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create an MVC5, Razor Widget 2 26
Do alert on select 6 14
Autocomplete with Jquery Question 2 20
I need help adding validation to my MVC.Net view 6 8
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 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