Is it possible to create validation controls in a code behind file?
Posted on 2006-07-14
I want to use ASP.NET 2.0 validation controls. However, I don't want to place the validation tags (ie: <asp:RequiredFieldValidator ID="valFirstName" runat="server"></asp:RequiredFieldValidator>) in my aspx file. I want to create validator objects in my code behind file to keep my validation seperate and tidy.
I've tried placing the following code in the page load event, but the Validate() method throws an error (System.NullReferenceException: Object reference not set to an instance of an object.).
Dim valFirstName As New UI.WebControls.RequiredFieldValidator()
valFirstName.EnableClientScript = False
valFirstName.ControlToValidate = "txtFirstName"
Is this possible? How can I do it? Or am I stuck placing the asp tags in my aspx file?