What is wrong with this code? please

A

Annie

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 ...
It compiles fine but when i press the button it riases the error.

Line 64: if (chk.Checked)????

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

nikki

You're missing brackets. The code is running for all controls, not just
checkboxes.
(I don't think C# supports "is", but this looks like C#...?)

foreach (Control ctl in this.Controls)
{
if (ctl is CheckBox)
{
chk = (CheckBox) ctl;
if (chk.Checked)
{
Response.Write(chk.Text);
}
}
}
 
A

Annie

thanks Nikki,

I realised about it last night. Now it doesn't raise the error. But this
time it does nothing actually.
Maybe the problem is the "is" keyword???

How do you loop through all the controls in C# and check for specific
control?

Bit conusing
 
N

nikki

Annie said:
thanks Nikki,

I realised about it last night. Now it doesn't raise the error. But this
time it does nothing actually.
Maybe the problem is the "is" keyword???

I think so, but it's kind of odd that using it doesn't cause an
error...
I have this code that checks for the type of a control as a textbox
from a datagrid event. Maybe it will help you. (I'm still new to .net
myself)

if (e.Item.Cells[count].Controls[count2].GetType() == typeof
(System.Web.UI.WebControls.TextBox))
How do you loop through all the controls in C# and check for specific
control?

A specific named control? Use the FindControl method.
An example from my app:
ddl = (DropDownList) e.Item.FindControl("ddlCategory_Item");

Otherwise, I think you need to do something very similar to what you
have already.

Give this a shot and see if it works. Extra response.writes and a
counter added to trace execution and see what's going on.

Control chk;
int counter = 0;
foreach (Control ctl in this.Controls)
{
counter ++;
if (ctl.GetType() == System.Web.UI.WebControls.CheckBox)
{
chk = (CheckBox) ctl;
if (chk.Checked)
{
Response.Write(chk.Text);
}
else
{
Response.Write("not checked");
}
}
else
{
Response.Write ("Not a checkbox");
}
}
Response.Write ("Number of controls: " + counter);
 
A

Annie

it is working fine now ;-)

It has to be as: foreach (Control ctl in this.Controls.Control[0]) -->
Control[0] refers to Form1 as form1 is the child of this (page)
and all other controls exist in form1!!


nikki said:
thanks Nikki,

I realised about it last night. Now it doesn't raise the error. But this
time it does nothing actually.
Maybe the problem is the "is" keyword???

I think so, but it's kind of odd that using it doesn't cause an
error...
I have this code that checks for the type of a control as a textbox
from a datagrid event. Maybe it will help you. (I'm still new to .net
myself)

if (e.Item.Cells[count].Controls[count2].GetType() == typeof
(System.Web.UI.WebControls.TextBox))
How do you loop through all the controls in C# and check for specific
control?

A specific named control? Use the FindControl method.
An example from my app:
ddl = (DropDownList) e.Item.FindControl("ddlCategory_Item");

Otherwise, I think you need to do something very similar to what you
have already.

Give this a shot and see if it works. Extra response.writes and a
counter added to trace execution and see what's going on.

Control chk;
int counter = 0;
foreach (Control ctl in this.Controls)
{
counter ++;
if (ctl.GetType() == System.Web.UI.WebControls.CheckBox)
{
chk = (CheckBox) ctl;
if (chk.Checked)
{
Response.Write(chk.Text);
}
else
{
Response.Write("not checked");
}
}
else
{
Response.Write ("Not a checkbox");
}
}
Response.Write ("Number of controls: " + counter);
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top