FPIT
asked on
How do i access a radio button control that i dynamically added on my code behind page to find out if it's checked or not?
I have successfully created the code to generate a number of radiobuttons dynamically in my app. The problem i'm having now is detecting which radiobuttons that have been checked by the user when they click the "Next" button.
Any ideas?
Thanks
Any ideas?
Thanks
by you mentioning the codebehind, im assuming you are talking about ASP .NET and code behind in VB .NET. for that purpose on you radio button,
set the AutoPostBack property to true
rdoTest.AutoPostback = true
and to recieve events,
do
AddHandler rdoTest.CheckChanged, AddressOf theFunctionThtShouldRunWhe nTheEventO ccurs
here u need to write this
function theFunctionThtShouldRunWhe nTheEventO ccurs(send er as Object, e as EventArgs)
....
end function
which takes care of what happens when the event occurs.
hope this helps
set the AutoPostBack property to true
rdoTest.AutoPostback = true
and to recieve events,
do
AddHandler rdoTest.CheckChanged, AddressOf theFunctionThtShouldRunWhe
here u need to write this
function theFunctionThtShouldRunWhe
....
end function
which takes care of what happens when the event occurs.
hope this helps
ASKER
Yes sorry about that...i'm using ASP.NET (VB.NET for the code-behind pages). I'm generating a set of Radio Buttons on the fly from the code-behind page because the number of radio buttons being used (if any) solely depends on who's using the system. So i need to create these buttons on the fly instead of hard coding in a set of buttons like "ldbkutty" suggested above. I have no problem creating the buttons, but i can't seem to figure out for the life of me how to capture which buttons where checked. I've tried the autopostback = true property, which works fine, but i don't know how to add the event handler to trigger my function when a button is clicked. Ordinarily you see the CheckedChanged event property in the intellisense, but i am not getting any events whatsoever to choose from in the code when i'm creating the button. So how do i add the event handler to each radio button to call the function that you've written above? I'll give you some of my code to look at, hopefully that will help out and not confuse anyone.....
So basically i find out how many radio buttons this user will be using by querying the database....here's the SqlDataReader loop that iterates through each record producing a radio button group (3 buttons per group which represents Good, Needs Improvement and Not Applicable). Each of the buttons are added to a TableCell (td) which is then added to a TableRow (tr) and finally added to an ASP:Table.
Here's the code:
Do While topics.Read()
Dim tr2 As TableRow
Dim td2, td3, td4, td5 As TableCell
Dim lbl_topic As Label
Dim tbh_topic_id As TextBox
Dim rb1, rb2, rb3 As RadioButton
Dim rb1_name, rb2_name, rb3_name, rb_group_name As String
tr2 = New TableRow()
td2 = New TableCell()
td3 = New TableCell()
td4 = New TableCell()
td5 = New TableCell()
rb1 = New RadioButton()
rb2 = New RadioButton()
rb3 = New RadioButton()
rb1_name = "rb_good_" & topic_id
rb2_name = "rb_ni_" & topic_id
rb3_name = "rb_na_" & topic_id
rb_group_name = "rb_group_" & y
'Creating radionbutton 1
rb1.ID = rb1_name
rb1.GroupName = rb_group_name
rb1.AutoPostBack = True
'RIGHT HERE I SHOULD BE ABLE TO ADD THE EVENT HANDLER LIKE: rb1.CheckChanged("rb_Check Changed") or something to that effect...but can't!
'Creating radio button 2
rb2.ID = rb2_name
rb2.GroupName = rb_group_name
'Creating radio button 3
rb3.ID = rb3_name
rb3.GroupName = rb_group_name
'Assigning radiobuttons 1, 2 and 3 to a TableCell and formatting the tablecell's width etc...
td3.Controls.Add(rb1)
td3.Width = px.Percentage(10%)
td3.HorizontalAlign = HorizontalAlign.Center
td4.Controls.Add(rb2)
td4.Width = px.Percentage(25%)
td4.HorizontalAlign = HorizontalAlign.Center
td5.Controls.Add(rb3)
td5.Width = px.Percentage(15%)
td5.HorizontalAlign = HorizontalAlign.Center
tr2.Controls.Add(td2)
tr2.Controls.Add(td3)
tr2.Controls.Add(td4)
tr2.Controls.Add(td5)
table.Controls.Add(tr2)
y += 1
Loop
That's how i created the radio buttons....now i need to know how to find out which ones where selected when the user clicks the Next button....i'd prefer not to autopostback on each click if possible. Ideally i want to check each button once when the "Next" button is clicked.
Any help would be greatly appreciated...thanks
So basically i find out how many radio buttons this user will be using by querying the database....here's the SqlDataReader loop that iterates through each record producing a radio button group (3 buttons per group which represents Good, Needs Improvement and Not Applicable). Each of the buttons are added to a TableCell (td) which is then added to a TableRow (tr) and finally added to an ASP:Table.
Here's the code:
Do While topics.Read()
Dim tr2 As TableRow
Dim td2, td3, td4, td5 As TableCell
Dim lbl_topic As Label
Dim tbh_topic_id As TextBox
Dim rb1, rb2, rb3 As RadioButton
Dim rb1_name, rb2_name, rb3_name, rb_group_name As String
tr2 = New TableRow()
td2 = New TableCell()
td3 = New TableCell()
td4 = New TableCell()
td5 = New TableCell()
rb1 = New RadioButton()
rb2 = New RadioButton()
rb3 = New RadioButton()
rb1_name = "rb_good_" & topic_id
rb2_name = "rb_ni_" & topic_id
rb3_name = "rb_na_" & topic_id
rb_group_name = "rb_group_" & y
'Creating radionbutton 1
rb1.ID = rb1_name
rb1.GroupName = rb_group_name
rb1.AutoPostBack = True
'RIGHT HERE I SHOULD BE ABLE TO ADD THE EVENT HANDLER LIKE: rb1.CheckChanged("rb_Check
'Creating radio button 2
rb2.ID = rb2_name
rb2.GroupName = rb_group_name
'Creating radio button 3
rb3.ID = rb3_name
rb3.GroupName = rb_group_name
'Assigning radiobuttons 1, 2 and 3 to a TableCell and formatting the tablecell's width etc...
td3.Controls.Add(rb1)
td3.Width = px.Percentage(10%)
td3.HorizontalAlign = HorizontalAlign.Center
td4.Controls.Add(rb2)
td4.Width = px.Percentage(25%)
td4.HorizontalAlign = HorizontalAlign.Center
td5.Controls.Add(rb3)
td5.Width = px.Percentage(15%)
td5.HorizontalAlign = HorizontalAlign.Center
tr2.Controls.Add(td2)
tr2.Controls.Add(td3)
tr2.Controls.Add(td4)
tr2.Controls.Add(td5)
table.Controls.Add(tr2)
y += 1
Loop
That's how i created the radio buttons....now i need to know how to find out which ones where selected when the user clicks the Next button....i'd prefer not to autopostback on each click if possible. Ideally i want to check each button once when the "Next" button is clicked.
Any help would be greatly appreciated...thanks
ASKER
I should also tell you that the ASP:Table that holds the TableRow and TableCell which holds the Radio Buttons that i've created are added to a PlaceHolder that i have in the HTML page for example:
<asp:PlaceHolder id="ph_form" Runat="server"></asp:Place Holder>
The line above sits in my Form.aspx html page and i add the table with my radio buttons in it to this place holder. So i've tried accessing the radio buttons by using the FindControl function on the ph_form place holder field too, but no luck. For example, in my code-behind page i have an onClick function that is called when the user clicks the "Next" button that i have created...here's the code from that page that i've tried with no luck as well....perhaps that help too...
Sub saveForm(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEv entArgs)
Dim rb1, rb2, rb3 As RadioButton
Dim gradeType As String
Dim topics As SqlDataReader
Dim property_id, section_id, curPage As Integer
Dim retVal As Boolean
'rb1 = New RadioButton()
'rb2 = New RadioButton()
'rb3 = New RadioButton()
property_id = Session.Item("property_id" )
curPage = currentPage()
section_id = mod_info.getPropertyForm(p roperty_id , curPage)
topics = mod_info.getForm(property_ id, section_id)
gradeType = Session.Item("gradeType")
Do While topics.Read()
rb1 = ph_form.FindControl("rb_go od_" & topics(0))
Response.Write("rb_good_" & topics(0))
rb1 = CType(FindControl("rb_good _" & topics(0)).Controls(0), RadioButton)
rb2 = ph_form.FindControl("rb_ni _" & topics(0))
rb3 = ph_form.FindControl("rb_na _" & topics(0))
Response.Write(rb1)
If rb1.Checked = True Then
Response.Write("It's checked")
Else
Response.Write("It's NOT checked")
End If
Loop
ib_move_next()
End Sub
I've tried several different ways as you can see above, plus others and i can't get it to work. The above code may produce an error, i'm not sure i had most of it commented out as i've been trying different scenarios. I just figured i could create a new instance of a radio button rb1 (above) and use the FindControl function to pass the values from the already created radio button to the new instance and see if it's checked that way.....Hopefully this isn't confusing you.
Any ideas?
Thanks again....
<asp:PlaceHolder id="ph_form" Runat="server"></asp:Place
The line above sits in my Form.aspx html page and i add the table with my radio buttons in it to this place holder. So i've tried accessing the radio buttons by using the FindControl function on the ph_form place holder field too, but no luck. For example, in my code-behind page i have an onClick function that is called when the user clicks the "Next" button that i have created...here's the code from that page that i've tried with no luck as well....perhaps that help too...
Sub saveForm(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEv
Dim rb1, rb2, rb3 As RadioButton
Dim gradeType As String
Dim topics As SqlDataReader
Dim property_id, section_id, curPage As Integer
Dim retVal As Boolean
'rb1 = New RadioButton()
'rb2 = New RadioButton()
'rb3 = New RadioButton()
property_id = Session.Item("property_id"
curPage = currentPage()
section_id = mod_info.getPropertyForm(p
topics = mod_info.getForm(property_
gradeType = Session.Item("gradeType")
Do While topics.Read()
rb1 = ph_form.FindControl("rb_go
Response.Write("rb_good_" & topics(0))
rb1 = CType(FindControl("rb_good
rb2 = ph_form.FindControl("rb_ni
rb3 = ph_form.FindControl("rb_na
Response.Write(rb1)
If rb1.Checked = True Then
Response.Write("It's checked")
Else
Response.Write("It's NOT checked")
End If
Loop
ib_move_next()
End Sub
I've tried several different ways as you can see above, plus others and i can't get it to work. The above code may produce an error, i'm not sure i had most of it commented out as i've been trying different scenarios. I just figured i could create a new instance of a radio button rb1 (above) and use the FindControl function to pass the values from the already created radio button to the new instance and see if it's checked that way.....Hopefully this isn't confusing you.
Any ideas?
Thanks again....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And by Name of the Radio button i mean the id of it, not "name" litereally. Sorry if that caused any confusion.
And for the other part: event handlers are not added the way mentioned
<quote>
rb1.AutoPostBack = True
'RIGHT HERE I SHOULD BE ABLE TO ADD THE EVENT HANDLER LIKE: rb1.CheckChanged("rb_Check Changed") or something to that effect...but can't!
</quote>
the syntax is
AddHandler rb1.CheckChanged, AddressOf rb_checkchanged 'This is the exact syntax
<quote>
rb1.AutoPostBack = True
'RIGHT HERE I SHOULD BE ABLE TO ADD THE EVENT HANDLER LIKE: rb1.CheckChanged("rb_Check
</quote>
the syntax is
AddHandler rb1.CheckChanged, AddressOf rb_checkchanged 'This is the exact syntax
ASKER
Awesome thanks...i'll try it out!
ASKER
Hey Vippex,
Thanks alot, i used the request.form("name of radio button group") and that worked. Nothing was returned when i used the actual id of each radio button as you suggested (or i think you suggested). So i tried using the radio button group name and it worked perfectly. I can't believe i didn't think to try that! I did try request.querystring and request.servervariables to see what that returned but had no luck becuase i don't think i realized that only the name of the button would be returned if it's selected. Anyhow, it works now thanks to you!
'Creating radionbutton 1
rb1.ID = rb1_name
rb1.GroupName = rb_group_name
I used the value of rb_group_name to get what i was looking for not the value of rb1_name. Also the AddHandler syntax worked perfectly too....thanks for that as well because i'm sure i'll need that somewhere along the way too!
Thanks again!
Thanks alot, i used the request.form("name of radio button group") and that worked. Nothing was returned when i used the actual id of each radio button as you suggested (or i think you suggested). So i tried using the radio button group name and it worked perfectly. I can't believe i didn't think to try that! I did try request.querystring and request.servervariables to see what that returned but had no luck becuase i don't think i realized that only the name of the button would be returned if it's selected. Anyhow, it works now thanks to you!
'Creating radionbutton 1
rb1.ID = rb1_name
rb1.GroupName = rb_group_name
I used the value of rb_group_name to get what i was looking for not the value of rb1_name. Also the AddHandler syntax worked perfectly too....thanks for that as well because i'm sure i'll need that somewhere along the way too!
Thanks again!
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function checkedRadio() {
var isChecked = false;
var myForm = document.myform;
for(var i=0; i<myForm.myTopics.length; i++) {
if(myForm.myTopics[i].chec
alert(myForm.myTopics[i].v
isChecked = true;
}
}
if(!isChecked)
alert("Nothing checked!");
}
//-->
</script>
</head>
<body>
<form name="myform" method="post" action="" >
<input type="radio" name="myTopics" value="php"> php
<input type="radio" name="myTopics" value="jsp"> jsp
<input type="radio" name="myTopics" value="javascript"> javascript
<input type="radio" name="myTopics" value="mysql"> mysql
<input type="button" value="Next" onclick="checkedRadio()"/>
</form>
</body>
</html>
If server side, whatever you "checked" only be passed in the next page. For example,
in PHP:
$checkedRadio = isset($_POST["myTopics"]) ? $_POST["myTopics"] : "";
in JSP:
String checkedRadio = (request.getParameter("myT