Home All Groups Group Topic Archive Search About

Odd Validator Problem

Author
10 Apr 2010 5:08 PM
Gary Wardell
Hi,

I seem to have hit an odd problem.

I have the need to sometimes validate and sometimes not based on conditions
in a database.

However, the following page, which simulates the problem, works fine as a
stand alone page, but fails at the indicated line when used with a matter
page when clicking the NoVal button.  It also fails when clicking the val
button, but only when with a master page.

I have no idea why or how to fix it.

Thanks for any help.

======================
<%@ Page Language="vb" AutoEventWireup="false"
MasterPageFile="~/CourtTech.Master" Codebehind="Test2.aspx.vb"
Inherits="TrueLog.Test2" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Canvas" runat="server">
   <div>
      <asp:TextBox ID="TextBox1" runat="server" />
      <asp:RequiredFieldValidator ID="TBVal" ControlToValidate="TextBox1"
runat="server" ErrorMessage="*Required" /><br />
      <asp:Button ID="Button1" runat="server" Text="Submit" /><br />
      <asp:Button ID="NoVal" CausesValidation="false" runat="server"
Text="NoVal" /><br />
      <asp:Button ID="Val" CausesValidation="false" runat="server"
Text="Val" />
   </div>
</asp:Content>

=========================
Public Partial Class Test2
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

End Sub

Private Sub NoVal_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles NoVal.Click
    DirectCast(Me.FindControl("TBVal"), RequiredFieldValidator).Enabled =
False    <--- System.NullReferenceException: Object reference not set to an
instance of an object.
End Sub

Private Sub Val_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Val.Click
    DirectCast(Me.FindControl("TBVal"), RequiredFieldValidator).Enabled =
True
End Sub

End Class

Author
10 Apr 2010 5:56 PM
Family Tree Mike
On 4/10/2010 1:08 PM, Gary Wardell wrote:
Show quoteHide quote
> Hi,
>
> I seem to have hit an odd problem.
>
> I have the need to sometimes validate and sometimes not based on conditions
> in a database.
>
> However, the following page, which simulates the problem, works fine as a
> stand alone page, but fails at the indicated line when used with a matter
> page when clicking the NoVal button.  It also fails when clicking the val
> button, but only when with a master page.
>
> I have no idea why or how to fix it.
>
> Thanks for any help.
>
> ======================
> <%@ Page Language="vb" AutoEventWireup="false"
> MasterPageFile="~/CourtTech.Master" Codebehind="Test2.aspx.vb"
> Inherits="TrueLog.Test2" Title="Untitled Page" %>
> <asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
> </asp:Content>
> <asp:Content ID="Content2" ContentPlaceHolderID="Canvas" runat="server">
>     <div>
>        <asp:TextBox ID="TextBox1" runat="server" />
>        <asp:RequiredFieldValidator ID="TBVal" ControlToValidate="TextBox1"
> runat="server" ErrorMessage="*Required" /><br />
>        <asp:Button ID="Button1" runat="server" Text="Submit" /><br />
>        <asp:Button ID="NoVal" CausesValidation="false" runat="server"
> Text="NoVal" /><br />
>        <asp:Button ID="Val" CausesValidation="false" runat="server"
> Text="Val" />
>     </div>
> </asp:Content>
>
> =========================
> Public Partial Class Test2
> Inherits System.Web.UI.Page
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
> Handles Me.Load
>
> End Sub
>
> Private Sub NoVal_Click(ByVal sender As Object, ByVal e As System.EventArgs)
> Handles NoVal.Click
>      DirectCast(Me.FindControl("TBVal"), RequiredFieldValidator).Enabled =
> False<--- System.NullReferenceException: Object reference not set to an
> instance of an object.
> End Sub
>
> Private Sub Val_Click(ByVal sender As Object, ByVal e As System.EventArgs)
> Handles Val.Click
>      DirectCast(Me.FindControl("TBVal"), RequiredFieldValidator).Enabled =
> True
> End Sub
>
> End Class
>
>
>
>

This is not really a vb.net problem, so an asp.net group such as
microsoft.public.inetserver.asp.general may be more helpful.

I do however know that when your page content is place inside an area on
the master page, the control names get prepended with something like
"ctl00$ContentPlaceHolder1$", so you need to search for the control with
that name.  Here is a link describing the scenario:
http://www.dev102.com/2008/03/17/find-controls-in-an-aspnet-page-with-a-masterpage/.


--
Mike
Author
10 Apr 2010 6:55 PM
Gary Wardell
Hi Mike,

Interesting.  I had not bumped up against this before.

I thought that the naming containers only came into effect on the client and
didn't impact the server.

I put an ID on the sounding div and set it to run on the server and it
worked.

Now it get it to work in my application.  There the valuator there is buried
in a template in a details view.  That's why I didn't post that code. :-)
And why I used the FindControl rather than a direct reference.

Btw, other findcontrols work fine as they are, based off the ID of the
detailsview.  It's just this validator that's giving the problem.

Thanks Much,

Gary

"Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message
news:Oj9rmcN2KHA.4332@TK2MSFTNGP02.phx.gbl...
> On 4/10/2010 1:08 PM, Gary Wardell wrote:

>
> This is not really a vb.net problem, so an asp.net group such as
> microsoft.public.inetserver.asp.general may be more helpful.
>
> I do however know that when your page content is place inside an area on
> the master page, the control names get prepended with something like
> "ctl00$ContentPlaceHolder1$", so you need to search for the control with
> that name.  Here is a link describing the scenario:
>
http://www.dev102.com/2008/03/17/find-controls-in-an-aspnet-page-with-a-masterpage/.
Show quoteHide quote
>
>
> --
> Mike