Loop through a Plain Radio Button in ASP.NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    Loop through a Plain Radio Button in ASP.NET

    I have a list of plain HTML radio buttons that I want to be able to
    loop through, get the values from them and insert them into a db. each
    one should be a separate record... Can anyone please give me some kind
    of example on how to do this???? I am doing this in asp.net VB.
    Please let me know if you need any additional information.

    Thanks

  • Scott M.

    #2
    Re: Loop through a Plain Radio Button in ASP.NET

    If they are plain HTML radio buttons, you can't loop through them in
    server-side code. It must be done from client-side code.


    "Brian" <bciarcia@pepeh azard.com> wrote in message
    news:1109213918 .751967.126580@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    >I have a list of plain HTML radio buttons that I want to be able to
    > loop through, get the values from them and insert them into a db. each
    > one should be a separate record... Can anyone please give me some kind
    > of example on how to do this???? I am doing this in asp.net VB.
    > Please let me know if you need any additional information.
    >
    > Thanks
    >[/color]


    Comment

    • MasterGaurav

      #3
      Re: Loop through a Plain Radio Button in ASP.NET

      Can you elaborate on looping through radio buttons?

      If the radio buttons are defined by you, why do you need to loop
      through them after submission? And you can select only one item in a
      radiobutton-group.

      Can you give an example of what you are trying to achieve? That may
      help in analysing the problem better.


      --
      Cheers,
      Gaurav Vaish


      --------------------------------

      Comment

      • Emad Al-Ashi

        #4
        Re: Loop through a Plain Radio Button in ASP.NET

        to get most of the help in this group, you better post a try of ur code.
        asking plain questions like this usually is missleading or says that you
        haven't tried anything in the first place.
        why don't u try to do it in ur own code and post ur code so u can have
        better guidance. i hope u find the answer as soon as possible ;)


        "Brian" <bciarcia@pepeh azard.com> wrote in message
        news:1109213918 .751967.126580@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        >I have a list of plain HTML radio buttons that I want to be able to
        > loop through, get the values from them and insert them into a db. each
        > one should be a separate record... Can anyone please give me some kind
        > of example on how to do this???? I am doing this in asp.net VB.
        > Please let me know if you need any additional information.
        >
        > Thanks
        >[/color]


        Comment

        • Brian Ciarcia

          #5
          Re: Loop through a Plain Radio Button in ASP.NET

          Thanks for the replies.... Ok.. I have 8 groups of questions. I'll post
          a sample of one group below. Depending on who logs in, will depend on
          how many groups of questions they will get. Once they check either the
          yes or no radio button for each question, the will submit the form. I
          will need to know how many questions there are, then loop through those
          questions and input them into the db. for example( if two of the
          questions were "the sky is blue" and "the ocean is green".. One record
          will be created for the first question, and another record will be
          created for the second question). I also need to know if any answers
          were answered yes, but that is a separate issue. Right now, I just need
          to know how to do this part... Here is a sample of the questions. These
          are being stored in a questions table in the db.
          -------------------------------------------------

          <table cellspacing="0" cellpadding="0" width="100%" border="0">
          <tbody>
          <tr>
          <th noWrap>
          <div align="center"> <span class="style3"> Coronary Artery Disease
          </span></div></th>
          <th><span class="style3"> YES</span></th>
          <th><span class="style3"> NO</span></th></tr>
          <tr>
          <td noWrap width="69%"><sp an class="style3"> Have you ever received a
          diagnosis of heart disease or coronary artery disease?</span></td>
          <td width="6%">
          <div align="center">
          <input type="radio" value="yes" name="CAD_1" /> </div></td>
          <td width="6%">
          <div align="center">
          <input type="radio" value="no" name="CAD_1" /> </div></td></tr>
          <tr>
          <td class="style3" noWrap>Have you ever had a heart attack?</td>
          <td>
          <div align="center">
          <input type="radio" value="yes" name="CAD_2" /> </div></td>
          <td>
          <div align="center">
          <input type="radio" value="no" name="CAD_2" /> </div></td></tr>
          <tr>
          <td class="style3" noWrap>Have you ever been treated for a blocked atery
          anywhere in your body? </td>
          <td>
          <div align="center">
          <input type="radio" value="yes" name="CAD_3" /> </div></td>
          <td>
          <div align="center">
          <input type="radio" value="no" name="CAD_3" /> </div></td></tr>
          <tr>
          <td class="style3" noWrap><span class="style4"> Have you ever been
          treated for chest pain caused by blockage of arteries in your heart?
          </span></td>
          <td>
          <div align="center">
          <input type="radio" value="yes" name="CAD_4" /> </div></td>
          <td>
          <div align="center">
          <input type="radio" value="no" name="CAD_4" /> </div></td></tr>
          <tr>
          <td class="style3" noWrap><span class="style4"> Have you ever had an
          abnormal exercise tolerance test (ETT, stress test) </span></td>
          <td>
          <div align="center">
          <input type="radio" value="yes" name="CAD_5" /> </div></td>
          <td>
          <div align="center">
          <input type="radio" value="no" name="CAD_5" />
          </div></td></tr></tbody></table>

          -------------------------------------------------

          I am just querying the db and grabbing the appropriate groups.. The
          names of the radios are important too, per the customers request.
          Another group would be CHF_1,CHF_2, etc.. If anyone knows a better way
          of doing this, I'll be glad to hear it... Let me know if you need
          anymore detail..

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Scott M.

            #6
            Re: Loop through a Plain Radio Button in ASP.NET

            If there are only 8 questions (and each radio button in a group are all
            named the same name), you don't need to loop at all. Just check all 8
            groups to see if a group has a value. If a group has a value, then that
            question was responded to.

            Or, since they are submitting the data to the server, you could just check
            the Request object in the same way to see what data was submitted.


            "Brian Ciarcia" <bciarcia@pepeh azard.com> wrote in message
            news:e%23hIL2lG FHA.1096@tk2msf tngp13.phx.gbl. ..[color=blue]
            > Thanks for the replies.... Ok.. I have 8 groups of questions. I'll post
            > a sample of one group below. Depending on who logs in, will depend on
            > how many groups of questions they will get. Once they check either the
            > yes or no radio button for each question, the will submit the form. I
            > will need to know how many questions there are, then loop through those
            > questions and input them into the db. for example( if two of the
            > questions were "the sky is blue" and "the ocean is green".. One record
            > will be created for the first question, and another record will be
            > created for the second question). I also need to know if any answers
            > were answered yes, but that is a separate issue. Right now, I just need
            > to know how to do this part... Here is a sample of the questions. These
            > are being stored in a questions table in the db.
            > -------------------------------------------------
            >
            > <table cellspacing="0" cellpadding="0" width="100%" border="0">
            > <tbody>
            > <tr>
            > <th noWrap>
            > <div align="center"> <span class="style3"> Coronary Artery Disease
            > </span></div></th>
            > <th><span class="style3"> YES</span></th>
            > <th><span class="style3"> NO</span></th></tr>
            > <tr>
            > <td noWrap width="69%"><sp an class="style3"> Have you ever received a
            > diagnosis of heart disease or coronary artery disease?</span></td>
            > <td width="6%">
            > <div align="center">
            > <input type="radio" value="yes" name="CAD_1" /> </div></td>
            > <td width="6%">
            > <div align="center">
            > <input type="radio" value="no" name="CAD_1" /> </div></td></tr>
            > <tr>
            > <td class="style3" noWrap>Have you ever had a heart attack?</td>
            > <td>
            > <div align="center">
            > <input type="radio" value="yes" name="CAD_2" /> </div></td>
            > <td>
            > <div align="center">
            > <input type="radio" value="no" name="CAD_2" /> </div></td></tr>
            > <tr>
            > <td class="style3" noWrap>Have you ever been treated for a blocked atery
            > anywhere in your body? </td>
            > <td>
            > <div align="center">
            > <input type="radio" value="yes" name="CAD_3" /> </div></td>
            > <td>
            > <div align="center">
            > <input type="radio" value="no" name="CAD_3" /> </div></td></tr>
            > <tr>
            > <td class="style3" noWrap><span class="style4"> Have you ever been
            > treated for chest pain caused by blockage of arteries in your heart?
            > </span></td>
            > <td>
            > <div align="center">
            > <input type="radio" value="yes" name="CAD_4" /> </div></td>
            > <td>
            > <div align="center">
            > <input type="radio" value="no" name="CAD_4" /> </div></td></tr>
            > <tr>
            > <td class="style3" noWrap><span class="style4"> Have you ever had an
            > abnormal exercise tolerance test (ETT, stress test) </span></td>
            > <td>
            > <div align="center">
            > <input type="radio" value="yes" name="CAD_5" /> </div></td>
            > <td>
            > <div align="center">
            > <input type="radio" value="no" name="CAD_5" />
            > </div></td></tr></tbody></table>
            >
            > -------------------------------------------------
            >
            > I am just querying the db and grabbing the appropriate groups.. The
            > names of the radios are important too, per the customers request.
            > Another group would be CHF_1,CHF_2, etc.. If anyone knows a better way
            > of doing this, I'll be glad to hear it... Let me know if you need
            > anymore detail..
            >
            > *** Sent via Developersdex http://www.developersdex.com ***
            > Don't just participate in USENET...get rewarded for it![/color]


            Comment

            • Brian Ciarcia

              #7
              Re: Loop through a Plain Radio Button in ASP.NET

              Actually, it is 8 groups of questions and the names are different..
              (ie.. group one.. the names are CAD_1,CAD_2,CAD _3 group two.. the names
              are CHF_1,CHF_2,CHF _3,CHF_4. etc..) Each group has a different number
              of questions. Each question has to have an answer, either yes or no..
              Since this is an asp.net page, I have no clue How to get the answers
              into the database. CAD_1 has to have its own record, CAD_2 has to have
              its own record, etc, etc.


              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Emad Al-Ashi

                #8
                Re: Loop through a Plain Radio Button in ASP.NET

                try to put the group of radio buttons in a web control PlaceHolder, make
                those radio buttons Web controls instead of HTML in that PlaceHolder.

                then you can use :

                foreach( Control c in placeHolder.Con trols )
                {
                //cast the control to web radio button and do what ever u like with it
                }

                i hope this helps


                "Brian Ciarcia" <bciarcia@pepeh azard.com> wrote in message
                news:e%23hIL2lG FHA.1096@tk2msf tngp13.phx.gbl. ..[color=blue]
                > Thanks for the replies.... Ok.. I have 8 groups of questions. I'll post
                > a sample of one group below. Depending on who logs in, will depend on
                > how many groups of questions they will get. Once they check either the
                > yes or no radio button for each question, the will submit the form. I
                > will need to know how many questions there are, then loop through those
                > questions and input them into the db. for example( if two of the
                > questions were "the sky is blue" and "the ocean is green".. One record
                > will be created for the first question, and another record will be
                > created for the second question). I also need to know if any answers
                > were answered yes, but that is a separate issue. Right now, I just need
                > to know how to do this part... Here is a sample of the questions. These
                > are being stored in a questions table in the db.
                > -------------------------------------------------
                >
                > <table cellspacing="0" cellpadding="0" width="100%" border="0">
                > <tbody>
                > <tr>
                > <th noWrap>
                > <div align="center"> <span class="style3"> Coronary Artery Disease
                > </span></div></th>
                > <th><span class="style3"> YES</span></th>
                > <th><span class="style3"> NO</span></th></tr>
                > <tr>
                > <td noWrap width="69%"><sp an class="style3"> Have you ever received a
                > diagnosis of heart disease or coronary artery disease?</span></td>
                > <td width="6%">
                > <div align="center">
                > <input type="radio" value="yes" name="CAD_1" /> </div></td>
                > <td width="6%">
                > <div align="center">
                > <input type="radio" value="no" name="CAD_1" /> </div></td></tr>
                > <tr>
                > <td class="style3" noWrap>Have you ever had a heart attack?</td>
                > <td>
                > <div align="center">
                > <input type="radio" value="yes" name="CAD_2" /> </div></td>
                > <td>
                > <div align="center">
                > <input type="radio" value="no" name="CAD_2" /> </div></td></tr>
                > <tr>
                > <td class="style3" noWrap>Have you ever been treated for a blocked atery
                > anywhere in your body? </td>
                > <td>
                > <div align="center">
                > <input type="radio" value="yes" name="CAD_3" /> </div></td>
                > <td>
                > <div align="center">
                > <input type="radio" value="no" name="CAD_3" /> </div></td></tr>
                > <tr>
                > <td class="style3" noWrap><span class="style4"> Have you ever been
                > treated for chest pain caused by blockage of arteries in your heart?
                > </span></td>
                > <td>
                > <div align="center">
                > <input type="radio" value="yes" name="CAD_4" /> </div></td>
                > <td>
                > <div align="center">
                > <input type="radio" value="no" name="CAD_4" /> </div></td></tr>
                > <tr>
                > <td class="style3" noWrap><span class="style4"> Have you ever had an
                > abnormal exercise tolerance test (ETT, stress test) </span></td>
                > <td>
                > <div align="center">
                > <input type="radio" value="yes" name="CAD_5" /> </div></td>
                > <td>
                > <div align="center">
                > <input type="radio" value="no" name="CAD_5" />
                > </div></td></tr></tbody></table>
                >
                > -------------------------------------------------
                >
                > I am just querying the db and grabbing the appropriate groups.. The
                > names of the radios are important too, per the customers request.
                > Another group would be CHF_1,CHF_2, etc.. If anyone knows a better way
                > of doing this, I'll be glad to hear it... Let me know if you need
                > anymore detail..
                >
                > *** Sent via Developersdex http://www.developersdex.com ***
                > Don't just participate in USENET...get rewarded for it![/color]


                Comment

                • Brian Ciarcia

                  #9
                  Re: Loop through a Plain Radio Button in ASP.NET

                  Im sorry.. I'm really ignorant when it comes to this... How do I put it
                  in a placeholder? can I still store the groups in the database?? I
                  feel like such an idiot now..


                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • Emad Al-Ashi

                    #10
                    Re: Loop through a Plain Radio Button in ASP.NET

                    you are not idiot at all, all what you need is more reading. go to the msdn
                    and read more about the place holder :)


                    "Brian Ciarcia" <bciarcia@pepeh azard.com> wrote in message
                    news:egVPsonGFH A.3876@TK2MSFTN GP14.phx.gbl...[color=blue]
                    > Im sorry.. I'm really ignorant when it comes to this... How do I put it
                    > in a placeholder? can I still store the groups in the database?? I
                    > feel like such an idiot now..
                    >
                    >
                    > *** Sent via Developersdex http://www.developersdex.com ***
                    > Don't just participate in USENET...get rewarded for it![/color]


                    Comment

                    • Scott M.

                      #11
                      Re: Loop through a Plain Radio Button in ASP.NET

                      Brian,

                      If you use standard HTML radio buttons (<INPUT TYPE="radio">), then you
                      should name all the radio buttons for a given question the same name. That
                      way each question will have exactly ONE radio button that holds the answer
                      to the question. With standard HTML radio buttons, this is also what gives
                      them their mutual exclusivity.

                      As I said earlier, since the answers to all the questions that were asked
                      are going to be sent to the server via a submit, you can just query the
                      Request object to see what data it contains. By doing this, you will know
                      which questions were asked since only those answers would have been sent to
                      the server.

                      [VB.NET]
                      Dim answer as String
                      For Each Answer In Request.Form
                      'references to answer return the name of the item
                      'references to Request.Form(an swer) refer to the value of the item
                      Next

                      -Scott

                      "Brian Ciarcia" <bciarcia@pepeh azard.com> wrote in message
                      news:OkmAyXnGFH A.2420@TK2MSFTN GP14.phx.gbl...[color=blue]
                      > Actually, it is 8 groups of questions and the names are different..
                      > (ie.. group one.. the names are CAD_1,CAD_2,CAD _3 group two.. the names
                      > are CHF_1,CHF_2,CHF _3,CHF_4. etc..) Each group has a different number
                      > of questions. Each question has to have an answer, either yes or no..
                      > Since this is an asp.net page, I have no clue How to get the answers
                      > into the database. CAD_1 has to have its own record, CAD_2 has to have
                      > its own record, etc, etc.
                      >
                      >
                      > *** Sent via Developersdex http://www.developersdex.com ***
                      > Don't just participate in USENET...get rewarded for it![/color]


                      Comment

                      • Brian Ciarcia

                        #12
                        Re: Loop through a Plain Radio Button in ASP.NET

                        Scott,

                        Thanks... I swear I tried something like that before, but it didn't
                        work.. I just tried it using your code and it works great.. So now I am
                        looping through the questions when I submit to another page. Can I put
                        that code in the codebehind on this page, or does this form have to be
                        submitted to another page? If so, can I add this code to the codebehind
                        on the submission page???



                        *** Sent via Developersdex http://www.developersdex.com ***
                        Don't just participate in USENET...get rewarded for it!

                        Comment

                        • Scott M.

                          #13
                          Re: Loop through a Plain Radio Button in ASP.NET

                          In ASP.NET server-forms submit to the same page. You can add this code to
                          whatever code-behind you submit to.

                          You may want to use the Web Forms Panel control to hold the 8 groups of
                          questions (you'll need 8 panel controls). Then it is simply a matter of
                          setting the visible property of the correct panel to true and all others to
                          false. This way you can do all your work in one .aspx page with just one
                          code-behind.



                          "Brian Ciarcia" <bciarcia@pepeh azard.com> wrote in message
                          news:egrJPKoGFH A.3628@TK2MSFTN GP15.phx.gbl...[color=blue]
                          > Scott,
                          >
                          > Thanks... I swear I tried something like that before, but it didn't
                          > work.. I just tried it using your code and it works great.. So now I am
                          > looping through the questions when I submit to another page. Can I put
                          > that code in the codebehind on this page, or does this form have to be
                          > submitted to another page? If so, can I add this code to the codebehind
                          > on the submission page???
                          >
                          >
                          >
                          > *** Sent via Developersdex http://www.developersdex.com ***
                          > Don't just participate in USENET...get rewarded for it![/color]


                          Comment

                          • Brian Ciarcia

                            #14
                            Re: Loop through a Plain Radio Button in ASP.NET


                            Ok.. I'll look into the web form panel... do you know where I can find
                            any reading on that??


                            *** Sent via Developersdex http://www.developersdex.com ***
                            Don't just participate in USENET...get rewarded for it!

                            Comment

                            • Brian Ciarcia

                              #15
                              Re: Loop through a Plain Radio Button in ASP.NET


                              Ok.. I'll look into the web form panel... do you know where I can find
                              any reading on that??


                              *** Sent via Developersdex http://www.developersdex.com ***
                              Don't just participate in USENET...get rewarded for it!

                              Comment

                              Working...