I've got a page with a ton of checkBoxes on it. I would like to have a loop that can can just go through all of them but I can't get it to work. I know similar code works for a desktop application but apprently not for an ASP page. Here's what I'm trying:
ForEach
myControl AsControlIn
Page.Controls
IfTypeOf
myControl IsCheckBoxThen
Dim chkBox
AsCheckBox
= CType(myControl,
CheckBox)
hi, insteadof using Page.Controls try with form1.Controls i.e your form name
refer below code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GetControls.aspx.vb" Inherits="VBPage_GetControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:CheckBox ID="CheckBox2" runat="server" />
<asp:CheckBox ID="CheckBox3" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each myControl As Control In Me.form1.Controls
If TypeOf myControl Is CheckBox Then
Dim chkBox As CheckBox = CType(myControl, CheckBox)
If chkBox.Checked = True Then
Response.Write(chkBox.Checked)
End If
End If
Next
End Sub
Yeah that's what I was trying and it wasn't working. I just loaded all the checkBoxes into an array and looped through them that way. It was a little bit of a pain but it worked. Thanks anyway
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim MyItem as ListItem
lblMessage.Text = ""
For Each MyItem in cbl1.Items
If MyItem.Selected = True Then
lblMessage.Text = lblMessage.Text _
& MyItem.Text & "<BR>"
End If
Next
End Sub
Thanks for the response but I still get an NullReferenceException with that code on the For Each statement. That code would work but my check box list is in a formView and I don't know how to reference it.
Dim chkl As CheckBoxList = DirectCast(frm.FindControl("checkboxlist1"), CheckBoxList)
For Each li As ListItem In chkl.Items
If li.Selected Then
'do someting'
End If
Next
End Sub
ikeAndy
Member
40 Points
29 Posts
Looping through controls
Feb 22, 2012 08:14 PM|LINK
I've got a page with a ton of checkBoxes on it. I would like to have a loop that can can just go through all of them but I can't get it to work. I know similar code works for a desktop application but apprently not for an ASP page. Here's what I'm trying:
For Each myControl As Control In Page.Controls
If TypeOf myControl Is CheckBox Then
Dim chkBox As CheckBox = CType(myControl, CheckBox)
If chkBox.Checked = True Then
'Code to store checked boxes here
End If
End If
Next
Any suggestions?
karthicks
All-Star
32082 Points
5522 Posts
Re: Looping through controls
Feb 23, 2012 05:57 AM|LINK
hi, insteadof using Page.Controls try with form1.Controls i.e your form name
refer below code
Karthick S
ikeAndy
Member
40 Points
29 Posts
Re: Looping through controls
Feb 23, 2012 02:48 PM|LINK
Yeah that's what I was trying and it wasn't working. I just loaded all the checkBoxes into an array and looped through them that way. It was a little bit of a pain but it worked. Thanks anyway
rajsedhain
Contributor
4181 Points
1041 Posts
Re: Looping through controls
Feb 24, 2012 03:21 PM|LINK
try something like this:
Sub SubmitBtn_Click(Sender As Object, E As EventArgs) Dim MyItem as ListItem lblMessage.Text = "" For Each MyItem in cbl1.Items If MyItem.Selected = True Then lblMessage.Text = lblMessage.Text _ & MyItem.Text & "<BR>" End If Next End Submore:
http://www.java2s.com/Code/ASP/Asp-Control/LoopthroughaspcheckboxlistVBnet.htm
Raj Sedhain
ikeAndy
Member
40 Points
29 Posts
Re: Looping through controls
Feb 24, 2012 03:32 PM|LINK
Thanks for the response but I still get an NullReferenceException with that code on the For Each statement. That code would work but my check box list is in a formView and I don't know how to reference it.
rajsedhain
Contributor
4181 Points
1041 Posts
Re: Looping through controls
Feb 24, 2012 06:47 PM|LINK
you have to find the control first:
Dim chkl As CheckBoxList = DirectCast(frm.FindControl("checkboxlist1"), CheckBoxList) For Each li As ListItem In chkl.Items If li.Selected Then 'do someting' End If Next End Subsee this thread:
http://stackoverflow.com/questions/1222029/how-do-you-save-items-from-a-checkboxlist-in-a-formview
Raj Sedhain