-1

Good day all. I have tried and failed to find an answer to a couple of errors I'm receiving with this small project I created. I have a master.masterpage and 3 other child pages (content). Each one has a different flavor of a form. All the others work fine. This particular content page is not giving me the results I want.

First, when I hit submit and there are no validation errors, I'd like the label populated with "Success!!". currently it does not display even though the fields are valid.

Second, I want to clear all textboxes with a clear button but receive the following error. "NullReferenceException was unhandled by user code."

Can someone explain where I need corrections with good learning points?

Here is my asp.net code and vb code.

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Page3.aspx.vb" Inherits="_Default" %>

Rental Car Application Sheet

<div id="button2">
    <asp:Button ID="Button3" runat="server" Text="Clear" />
</div>

Partial Class _Default
Inherits System.Web.UI.Page
Dim form1 As Control
Protected Sub Button2_Click(sender As Object, e As EventArgs, ByVal f As System.EventArgs)
    If Page.IsValid Then
        Label11.Text = "Success!!"
    End If
End Sub

Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    For Each Control In form1.Controls
        If TypeOf Control Is TextBox Then
            Control.Text = ""     'Clear all text
        End If
    Next Control
End Sub

End Class

allendks45
  • 339
  • 5
  • 17
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – zaggler Jan 31 '16 at 19:46
  • Although I'd like to fix it, i'd rather understand why i'm getting the error. – allendks45 Jan 31 '16 at 19:49
  • Visit the link I provided, it will explain more in detail as to why... You are trying to use an object when it's either nothing or null... – zaggler Jan 31 '16 at 19:53
  • Thanks! I'm reviewing the information. – allendks45 Jan 31 '16 at 19:59
  • Turn on Option Strict. `Text` is not a member of Web.UI.Control, so that wont compile. It is hard to tell what you mean to do with `Dim form1 As Control` – Ňɏssa Pøngjǣrdenlarp Jan 31 '16 at 21:23

1 Answers1

0

I would use me.controls instead of form1.controls. Me.Controls will get the current control collection

For Each Control In Me.Controls
Ken Tucker
  • 4,059
  • 1
  • 16
  • 24