burak Posted August 13, 2003 Posted August 13, 2003 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 Quote
Leaders dynamic_sysop Posted August 13, 2003 Leaders Posted August 13, 2003 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. Quote
burak Posted August 13, 2003 Author Posted August 13, 2003 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 Quote
Administrators PlausiblyDamp Posted August 13, 2003 Administrators Posted August 13, 2003 Is there a reason why you are adding the validator in code rather than the designer? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
burak Posted August 13, 2003 Author Posted August 13, 2003 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.. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.