Error when trying to loop through Form Controls


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Error when trying to loop through Form Controls

  1. #1
    Join Date
    Sep 2005
    Posts
    3

    Error when trying to loop through Form Controls

    I have a current form that has some code on the Submit_Click event and I recently added a Class reference at the top to validate the controls and if they fail highlight them. But now when i run the form it fails when it runs the following code:

    Sub Submit_Click(sender As Object, e As EventArgs)
    If Page.IsValid then

    Dim myForm As Control = Page.FindControl("security")
    Dim ctl1 As Control

    For Each ctl1 In myForm.Controls
    If ctl1.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then
    If CType(ctl1, TextBox).text = "" Then
    AutoEmail1.Ignorefieldlist += CType(ctl1, TextBox).id+","
    Else
    CType(ctl1, TextBox).Text = "I Ran"
    End If
    End If
    Next ctl1
    AutoEmail1.Initiate()
    End if
    End Sub


    It fails specifically on the For Each line. The error i get is:

    Object reference not set to an instance of an object.
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


    Is there a different way i have to write the syntax now for looping through the controls if im using a class to do some validation of them?

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Sounds like this line is failing:
    Dim myForm As Control = Page.FindControl("security")

    Are you sure that FindControl is returning a reference to your form?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Sep 2005
    Posts
    3
    EDIT: I fixed it, i had to add ID="myformname" to the form tag..

    Chris
    Last edited by oochr1soo; 09-16-2005 at 03:43 PM.

Similar Threads

  1. Perk: A good trick with Form Controls
    By bit_ravinder in forum .NET
    Replies: 0
    Last Post: 07-13-2005, 01:22 AM
  2. Search Label Controls in Web Form
    By Kee Peck in forum ASP.NET
    Replies: 2
    Last Post: 05-23-2002, 12:35 PM
  3. Access Web Form Controls
    By Cal in forum ASP.NET
    Replies: 1
    Last Post: 05-23-2002, 12:07 PM
  4. Accessing controls on a form
    By Brian in forum .NET
    Replies: 1
    Last Post: 03-25-2002, 12:48 PM
  5. Parsing Form Elements in a Loop
    By Brian in forum authorevents.mitchell
    Replies: 1
    Last Post: 10-16-2000, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center