Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > What is wrong with this code? please

Reply
Thread Tools

What is wrong with this code? please

 
 
Annie
Guest
Posts: n/a
 
      10-03-2005
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???


 
Reply With Quote
 
 
 
 
nikki
Guest
Posts: n/a
 
      10-03-2005
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);
}
}
}

 
Reply With Quote
 
 
 
 
Annie
Guest
Posts: n/a
 
      10-04-2005
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






"nikki" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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);
> }
> }
> }
>



 
Reply With Quote
 
nikki
Guest
Posts: n/a
 
      10-04-2005

Annie wrote:
> 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);

 
Reply With Quote
 
Annie
Guest
Posts: n/a
 
      10-09-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> Annie wrote:
>> 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);
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please please please help this guy with his open source java app casioculture@gmail.com Java 4 05-05-2005 08:24 AM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
Computer problems please please please help Nick Computer Support 0 06-04-2004 08:49 PM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments