Providing 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) RangeValidator objRangeValidator = new RangeValidator(); objRangeValidator.ControlToValidate = "txtPreviousCostToCompany"; objRangeValidator.MinimumValue = "0"; objRangeValidator.MaximumValue = "10"; objRangeValidator.Validate(); if(objRangeValidator.IsValid) lblMessage.text = objRangeValidator.IsValid.toString(); 1) but the validate method is generating error 2) If we don't provide the validate method the code is not having any effect
-
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) RangeValidator objRangeValidator = new RangeValidator(); objRangeValidator.ControlToValidate = "txtPreviousCostToCompany"; objRangeValidator.MinimumValue = "0"; objRangeValidator.MaximumValue = "10"; objRangeValidator.Validate(); if(objRangeValidator.IsValid) lblMessage.text = objRangeValidator.IsValid.toString(); 1) but the validate method is generating error 2) If we don't provide the validate method the code is not having any effect
lijukv wrote: but the validate method is generating error If you don't tell us what the error actually is then it makes it really difficult to help you. lijukv wrote: If we don't provide the validate method the code is not having any effect That is because your control is dynamic and you are constructing it, not ASP.NET. Remember that as soon as a page has been sent to the client browser all the objects on the page are destroyed, when user clicks a button to postback to the server it recreates all the objects and fires off the events. Because you are creating your control dynamically ASP.NET does not have any information (from the ASPX page) about the object and cannot fire off events for you. Therefore, you have to manually call Validate. It is possible to recreate your dynamic object in such a way that the events will fire normally but I don't recall off the top of my head.
Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
-
lijukv wrote: but the validate method is generating error If you don't tell us what the error actually is then it makes it really difficult to help you. lijukv wrote: If we don't provide the validate method the code is not having any effect That is because your control is dynamic and you are constructing it, not ASP.NET. Remember that as soon as a page has been sent to the client browser all the objects on the page are destroyed, when user clicks a button to postback to the server it recreates all the objects and fires off the events. Because you are creating your control dynamically ASP.NET does not have any information (from the ASPX page) about the object and cannot fire off events for you. Therefore, you have to manually call Validate. It is possible to recreate your dynamic object in such a way that the events will fire normally but I don't recall off the top of my head.
Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
:) RangeValidator objRangeValidator = new RangeValidator(); objRangeValidator.ControlToValidate = "txtPreviousCostToCompany"; objRangeValidator.Type = ValidationDataType.Integer; objRangeValidator.MaximumValue = "10"; objRangeValidator.MinimumValue = "0"; objRangeValidator.ErrorMessage = "Error"; objRangeValidator.Validate(); This is the error I am getting(I think for other validation controls also the same error) ------------------------------ 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 = "";