What is wrong with this code? please

Discussion in 'ASP .Net Web Controls' started by Annie, Oct 3, 2005.

  1. Annie

    Annie Guest

    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???
     
    Annie, Oct 3, 2005
    #1
    1. Advertising

  2. Annie

    nikki Guest

    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);
    }
    }
    }
     
    nikki, Oct 3, 2005
    #2
    1. Advertising

  3. Annie

    Annie Guest

    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" <> wrote in message
    news:...
    > 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);
    > }
    > }
    > }
    >
     
    Annie, Oct 4, 2005
    #3
  4. Annie

    nikki Guest

    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);
     
    nikki, Oct 4, 2005
    #4
  5. Annie

    Annie Guest

    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" <> wrote in message
    news:...
    >
    > 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);
    >
     
    Annie, Oct 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asfand Yar Qazi
    Replies:
    2
    Views:
    533
    Asfand Yar Qazi
    Sep 26, 2003
  2. KK
    Replies:
    2
    Views:
    595
    Big Brian
    Oct 14, 2003
  3. Claire
    Replies:
    5
    Views:
    834
    Bernardo Signori
    Jun 26, 2003
  4. jmdeschamps
    Replies:
    4
    Views:
    430
    jmdeschamps
    Jan 19, 2004
  5. funky
    Replies:
    5
    Views:
    55
    Mark H Harris
    May 28, 2014
Loading...

Share This Page