473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Looping through controls error?? please

hello guys,

I am getting the following error:
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

What i am trying to do is to loop through all of the controls on the page
and if they are Checkboxes and then see if the they are
checked then do something ...

private void Button1_Click(object sender, System.EventArgs e)

{

CheckBox chk=null;

foreach (Control ctl in this.Controls)

{
if (ctl is CheckBox)

chk = (CheckBox) ctl;

if (chk.Checked)

{

Response.Write(chk.Text);

}

}

}

i don't know what is wrong here???


Nov 19 '05 #1
4 1059
Hi,

Could you tell - what code line results in exception ?

Kalpesh

Nov 19 '05 #2
It compiles fine. but when i press the button it raises the error.

Line 64: if (chk.Checked)

Anyways, how would you loop though all your controls in a page and check for
some types eg CheckBox or Button etc

Ta

"Kalpesh" <sh*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi,

Could you tell - what code line results in exception ?

Kalpesh

Nov 19 '05 #3
Hi,
U have used correctly....but one small mistake...u dint use "{ }" ie
brackets after the statement if(ctl is CheckBox)....

but u will not get just with this.Controls in for loop..
the whole code should look like..

foreach(Control ctl in this.Controls[1].Controls){
if (ctl is CheckBox){
ch = (CheckBox)ctl;
Response.Write(ch.Text + " "); .......include whatever u want
}
}
Controls[1] refers to the form
It will fetch all the check boxes placed (directly) on the form.
....if u have placed check boxes in any data grid or in a data
list...then u need to use Recursion for traversing throught the control
hierarchy.
hope it helps,

Praveen P.

Nov 19 '05 #4
Thanks Paraveen!
Very nice code
all the bests!

"Praveen" <pr*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi,
U have used correctly....but one small mistake...u dint use "{ }" ie
brackets after the statement if(ctl is CheckBox)....

but u will not get just with this.Controls in for loop..
the whole code should look like..

foreach(Control ctl in this.Controls[1].Controls){
if (ctl is CheckBox){
ch = (CheckBox)ctl;
Response.Write(ch.Text + " "); .......include whatever u want
}
}
Controls[1] refers to the form
It will fetch all the check boxes placed (directly) on the form.
...if u have placed check boxes in any data grid or in a data
list...then u need to use Recursion for traversing throught the control
hierarchy.
hope it helps,

Praveen P.

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
4
by: Deasun O'Donnachadha | last post by:
Good evening, I have a panel with a number of labels and textboxes. I want to set the text boxes to readonly = false. This is what I have; Dim objTxtBox As New TextBox For Each objTxtBox In...
5
by: johnb41 | last post by:
I need to loop through a bunch of textbox controls on my form. The order of the loop is very important. For example, the top one must be read first, then the one below it, etc. My first...
7
by: astro | last post by:
I am not farmilar with the object model for webforms. I want to loop through the web form controls - pulling out the checkboxes on the form like the following: For Each ctrl In Me.Controls ...
15
by: Arpan | last post by:
Consider the following code which retrieves data from a SQL Server 2005 DB table & displays it in a DataGrid: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)...
2
by: pob | last post by:
Whats the difference between using a control or a listbox when looping thru a listbox. In example 1 it dims a listbox and an example 2 it dims a control. Please explain. Thanks in advance ...
0
by: rshafakian | last post by:
This is a bit weird. I'm working with a windows application using C#. I'm developing in Visual Studio.NET 2005. I'm using the card.dll that comes with all PC's just so you know. Each card that...
4
by: sparks | last post by:
well I am trying to get around looking at all the buttons with a case statement so I was trying this. Public Sub looking(ctlname) Dim ctl As OptionGroup Dim btn As ToggleButton ctl =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.