Solved

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

Posted on 2014-09-09
9
854 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

803 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