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

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
LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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
CipherISAuthor Commented:
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
käµfm³d 👽Commented:
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

CipherISAuthor Commented:
@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
käµfm³d 👽Commented:
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
CipherISAuthor Commented:
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
käµfm³d 👽Commented:
No, by "page life-cyle" I mean which event:

http://msdn.microsoft.com/en-us/library/ms178472(v=vs.85).aspx
0
Ramkisan JagtapLead DeveloperCommented:
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
Ramkisan JagtapLead DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.