Solved

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

Posted on 2014-09-09
9
820 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
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 1

Author Comment

by:CipherIS
Comment Utility
@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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

771 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

12 Experts available now in Live!

Get 1:1 Help Now