Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Providing Validation Control in Runtime

Providing Validation Control in Runtime

Scheduled Pinned Locked Moved ASP.NET
designhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lijukv
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • L lijukv

      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

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • C Colin Angus Mackay

        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

        L Offline
        L Offline
        lijukv
        wrote on last edited by
        #3

        :) 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 = "";

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups