Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I am trying to do validation in the code behind.

 

I have a textbox "txtCompAddrs"

 

Protected WithEvents txtCompAddrs As System.Web.UI.WebControls.TextBox

 

Here is my code to validate it

 

Dim x As RequiredFieldValidator

 

x.ControlToValidate = txtCompAddrs.ID

x.Validate()

 

When I run the page I get a

 

"Object reference not set to an instance of an object."

 

error for this line

 

x.ControlToValidate = txtCompAddrs.ID

 

If you look at,

 

http://www.aspalliance.com/aspxtrem...ToValidate.aspx

 

it says we are supposed to set the controltovalidate property to the object to be validated's id. I think my code is correct but it is not working.

 

How can I set the controltovalidate of a requiredfieldvalidator?

 

Burak

  • Leaders
Posted

have you tried swapping this line around

x.ControlToValidate = txtCompAddrs.ID

to

txtCompAddrs = new System.Web.UI.WebControls.TextBox
'/// make it a new instance.
txtCompAddrs.ID = x.ControlToValidate

because txtCompAddrs needs to be initialized.

Posted

Hi,

 

txtCompAddrs is a textbox on the html form, and it is the textbox i am checking to make sure it is filled in.

 

so why does it need to be initialized? I am confused about that.

 

the requiredfieldvalidator's controltovalidate property should be set to txtCompAddrs but so far I have not been able to figure out how to do it.

 

By the way, congratulations on your twins, I am an identical twin myself.

 

Burak

Posted

I am doing the validation in the code behind because I have a checkbox list control on the form, and the validation is only going to take place depending on which checkboxes are checked.

 

ie.. if the "Address" checkbox is checked I am going to validate the address, if the "Email" checkbox is checked I am going to validate the email etc..

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...