Loop through a Plain Radio Button in ASP.NET

B

Brian

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
 
S

Scott M.

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.
 
M

MasterGaurav

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
http://www.mastergaurav.org
http://mastergaurav.blogspot.com
--------------------------------
 
E

Emad Al-Ashi

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 ;)
 
B

Brian Ciarcia

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%"><span 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..
 
S

Scott M.

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.
 
B

Brian Ciarcia

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.
 
E

Emad Al-Ashi

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.Controls )
{
//cast the control to web radio button and do what ever u like with it
}

i hope this helps
 
B

Brian Ciarcia

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..
 
E

Emad Al-Ashi

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

Scott M.

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(answer) refer to the value of the item
Next

-Scott
 
B

Brian Ciarcia

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???
 
S

Scott M.

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.
 
B

Brian Ciarcia

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

Brian Ciarcia

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

Brian Ciarcia

If I use the panels, do I then have to change the radio buttons to
asp:radiobuttons or can I keep them as regular buttons?
 
B

Brian Ciarcia

ok.. i know you probably think I am a pain in the _ss right now, but to
control the visibility of those panels, would I do something like this?

I set the Panel ID's to be CAD, HB, CHF, DBTS. The customer I am
pulling up only should see CAD and HB.


foreach( Control c in Panel.Controls )

If (objBarDataReader("ProgramID") = Panel(c) then

Panel(c).visibility = "true"
end if

next

Is that Close?????
 
B

Brian Ciarcia

I got it.. I did this;


Dim ctl as Control

For Each ctl In Page.Controls
If TypeOf (ctl) Is System.Web.UI.WebControls.Panel Then

If ctl.ID = objBarDataReader("ProgramID") Then
ctl.visible = True
End If
End If
Next
 
B

Brian Ciarcia

I just added this code to my codebehind and now my questions wont
display.. Am I doing something wrong??

I created an asp:button called LoginButton and created a label called
errormessage.

I am just trying to display the results so I can see what is happening.

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

Private Sub LoginButton_Click(ByVal sender as System.Object, ByVal e as
System.EventArgs) Handles LoginButton.Click



Dim answer as String
Dim AnsStr
For Each Answer In Request.Form
'references to answer return the name of the item
AnsStr = AnsStr & "the answer to question " & answer & " is " &
Request.Form(answer) & "<br>"
Next
errormessage.Text = AnsStr


end sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top