NullReferenceException on Validate()

G

Guest

I have a custom composite control which has a validator for a textbox. The validator and textbox are declared in the class and created in the CreateChildControls() method

Here is the code for the textbox, in CreateChildControls()

searchBox = new TextBox();
searchBox.ID = "searchBox";
searchBox.EnableViewState = false;

It is then added to a TableCell, which is added to a row, etc.


Here is the validator being created (also in CreateChildControls()):

searchBoxValidator = new RequiredFieldValidator();
searchBoxValidator.ID = "searchBoxValidator";
searchBoxValidator.EnableViewState = false;
searchBoxValidator.ControlToValidate = "searchBox";
searchBoxValidator.ErrorMessage = "You must enter a product ID";

This validator, again, is added to a TableCell, etc.

On a submit button's click event, I call searchBoxValidator.Validate(), which throws a NullReferenceException:

[NullReferenceException: Object reference not set to an instance of an object.]
AdminToolProduct.ProductSelectControl.searchButtonClick(Object sender, EventArgs e) in c:\inetpub\wwwroot\admintoolproduct_1\controls\productselectcontrol.cs:235
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
....


Any ideas? I've tried setting the ControlToValidate property of the validator to
UniqueID+"_searchBox", resulting in the same error. Besides, if the control to validate is not found, I thought an HttpException was thrown. I also noticed that for some reason, no client side validation is done, even though I explicity tried setting the EnableClientScript to true (doesn't seem to be anything showing up in the source). Am I setting up this validator in the wrong place, or doing something else wrong? Thank you for your time.
 
J

John Saunders

Jeff Evans said:
I have a custom composite control which has a validator for a textbox.
The validator and textbox are declared in the class and created in the
CreateChildControls() method
Here is the code for the textbox, in CreateChildControls()

searchBox = new TextBox();
searchBox.ID = "searchBox";
searchBox.EnableViewState = false;

It is then added to a TableCell, which is added to a row, etc.


Here is the validator being created (also in CreateChildControls()):

searchBoxValidator = new RequiredFieldValidator();
searchBoxValidator.ID = "searchBoxValidator";
searchBoxValidator.EnableViewState = false;
searchBoxValidator.ControlToValidate = "searchBox";
searchBoxValidator.ErrorMessage = "You must enter a product ID";

This validator, again, is added to a TableCell, etc.

[NullReferenceException: Object reference not set to an instance of an object.]
AdminToolProduct.ProductSelectControl.searchButtonClick(Object sender,
EventArgs e) in
c:\inetpub\wwwroot\admintoolproduct_1\controls\productselectcontrol.cs:235
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
...


Any ideas? I've tried setting the ControlToValidate property of the validator to
UniqueID+"_searchBox", resulting in the same error. Besides, if the
control to validate is not found, I thought an HttpException was thrown. I
also noticed that for some reason, no client side validation is done, even
though I explicity tried setting the EnableClientScript to true (doesn't
seem to be anything showing up in the source). Am I setting up this
validator in the wrong place, or doing something else wrong? Thank you for
your time.
 
G

Guest

John Saunders said:
Perhaps you need to call EnsureChildControls in the Click event before
referencing other controls.

Thanks for the reply, but I have already called EnsureChildControls() in OnInit() (and I did try adding it in the click event just to be sure). I don't understand the NullReferenceException here, since the Validator itself is definitely instantiated at that point. The last function mentioned in the stack trace is:

System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName)

but the docs for it only mention throwing HttpException... I thought I had listed the stack trace before, but it looks like I didn't:


[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName)
System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid()
System.Web.UI.WebControls.BaseValidator.get_PropertiesValid()
System.Web.UI.WebControls.BaseValidator.Validate()


Any other suggestions? It's not that big of a deal, since one can accomplish the same thing as validators with a little extra code, but it still *should* work. Thanks.
 
G

Guest

John Saunders said:
Perhaps you need to call EnsureChildControls in the Click event before
referencing other controls.

Thanks for the reply, but I have already called EnsureChildControls() in OnInit() (and I did try adding it in the click event just to be sure). I don't understand the NullReferenceException here, since the Validator itself is definitely instantiated at that point. The last function mentioned in the stack trace is:

System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName)

but the docs for it only mention throwing HttpException... I thought I had listed the stack trace before, but it looks like I didn't:


[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName)
System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid()
System.Web.UI.WebControls.BaseValidator.get_PropertiesValid()
System.Web.UI.WebControls.BaseValidator.Validate()


Any other suggestions? It's not that big of a deal, since one can accomplish the same thing as validators with a little extra code, but it still *should* work. Thanks.
 
J

John Saunders

Jeff Evans said:
Thanks for the reply, but I have already called EnsureChildControls() in
OnInit() (and I did try adding it in the click event just to be sure). I
don't understand the NullReferenceException here, since the Validator itself
is definitely instantiated at that point. The last function mentioned in
the stack trace is:System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(Strin
g name, String propertyName)
but the docs for it only mention throwing HttpException... I thought I
had listed the stack trace before, but it looks like I didn't:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(Strin
g name, String propertyName)
System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid()
System.Web.UI.WebControls.BaseValidator.get_PropertiesValid()
System.Web.UI.WebControls.BaseValidator.Validate()


Any other suggestions? It's not that big of a deal, since one can
accomplish the same thing as validators with a little extra code, but it
still *should* work. Thanks.

One area to think about is, why do you need to manually call Validate? Why
isn't the page validation framework calling it? I ask because it may be that
you're calling Validate too early. If the page validation framework were to
call Validate for you, one would presume it would be done at the proper
time.
 
G

Guest

John Saunders said:
One area to think about is, why do you need to manually call Validate? Why
isn't the page validation framework calling it? I ask because it may be that
you're calling Validate too early. If the page validation framework were to
call Validate for you, one would presume it would be done at the proper
time.

I don't want all the validators to act at the Page level, as is discussed at:

http://www.peterblum.com/VAM/ValMain.aspx?ReturnUrl=Home.aspx

I don't understand how it could be too late. This is happening during the postback events, after Init and Load have completed for the composite and all the children. The only thing later is PreRender, save viewstate, and render.

I'll have to do some experimentation. Thanks for the reply.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top