Error while creating validation control in runtime
-
I want to use Range Validator for 3 text boxes, I don't want to provide three Range Validator in the Web form(ie no design time Range validator). I don't want to use CustomValidation control(since I have to provide the validation as a function) ----------------------------- Error Happens in the "Validate()" method. The code and Error description is given below RangeValidator objRangeValidator = new RangeValidator(); objRangeValidator.ControlToValidate = "txtPreviousCostToCompany"; objRangeValidator.Type = ValidationDataType.Integer; objRangeValidator.MaximumValue = "10"; objRangeValidator.MinimumValue = "0"; objRangeValidator.ErrorMessage = "Error"; objRangeValidator.Validate(); Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Source File: c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs Line: 238 Stack Trace: [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.RangeValidator.ControlPropertiesValid() System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() System.Web.UI.WebControls.BaseValidator.Validate() HRMG.Recruitment.recruitment.cmdSave_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs:238 System.Web.UI.WebControls.Button.onclick(EventArgs e) System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) System.Web.UI.Page.ProcessRequestMain() -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 236: objRangeValidator.MinimumValue = "0"; Line 237: objRangeValidator.ErrorMessage = "Error"; Line 238: objRangeValidator.Validate(); Line 239: Line 240: lblMessage.Text = ""; Thanks for reading this much
-
I want to use Range Validator for 3 text boxes, I don't want to provide three Range Validator in the Web form(ie no design time Range validator). I don't want to use CustomValidation control(since I have to provide the validation as a function) ----------------------------- Error Happens in the "Validate()" method. The code and Error description is given below RangeValidator objRangeValidator = new RangeValidator(); objRangeValidator.ControlToValidate = "txtPreviousCostToCompany"; objRangeValidator.Type = ValidationDataType.Integer; objRangeValidator.MaximumValue = "10"; objRangeValidator.MinimumValue = "0"; objRangeValidator.ErrorMessage = "Error"; objRangeValidator.Validate(); Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Source File: c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs Line: 238 Stack Trace: [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.RangeValidator.ControlPropertiesValid() System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() System.Web.UI.WebControls.BaseValidator.Validate() HRMG.Recruitment.recruitment.cmdSave_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\timesheetmanagement\recruitment.aspx.cs:238 System.Web.UI.WebControls.Button.onclick(EventArgs e) System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) System.Web.UI.Page.ProcessRequestMain() -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 236: objRangeValidator.MinimumValue = "0"; Line 237: objRangeValidator.ErrorMessage = "Error"; Line 238: objRangeValidator.Validate(); Line 239: Line 240: lblMessage.Text = ""; Thanks for reading this much
Hi there, Because the
RangeValidator
is dynamically created at runtime and it's not added to a container, so when you call theValidate
method, the error happens as the method uses the naming container to find the control which is specified in theControlToValidate
property in the control hierarchy. To work around this error, you simply add the validator to a container such as the Form element:RangeValidator objRangeValidator = new RangeValidator();
this.FindControl("Form1").Controls.Add(objRangeValidator);
...For more information, you can see Control.NamingContainer Property[^]