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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. CUSTOM WEB SERVER CONTROL THAT VALIDATE ITSELF

CUSTOM WEB SERVER CONTROL THAT VALIDATE ITSELF

Scheduled Pinned Locked Moved ASP.NET
csharpjavascriptdesignsysadmin
9 Posts 3 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.
  • J Offline
    J Offline
    JohnyCoder
    wrote on last edited by
    #1

    hello everybody: i am new to VB.net I have to design a custom web server control which validates itself on client side and server-side. which means that i would not use a seperate custom validator for validation, but it should be a part of the custom control itself.and this validation would be done on clientside first, and incase javascript is off in browser then validation is done on server side. The description of the custom control is as follows: its a date control which has 3 dropdowns(dds).one for month, second for days and third for days.it has two properties isDOB(boolean) and getDate which returns the date in the format mm/dd/yyyy. one method setDate which takes the string inn the format mm/dd/yyyy.This will set date according to the input string. can somebody point me to the rite direction.any help would be deeply appreciated. Thanks Cheers! AB

    I M 2 Replies Last reply
    0
    • J JohnyCoder

      hello everybody: i am new to VB.net I have to design a custom web server control which validates itself on client side and server-side. which means that i would not use a seperate custom validator for validation, but it should be a part of the custom control itself.and this validation would be done on clientside first, and incase javascript is off in browser then validation is done on server side. The description of the custom control is as follows: its a date control which has 3 dropdowns(dds).one for month, second for days and third for days.it has two properties isDOB(boolean) and getDate which returns the date in the format mm/dd/yyyy. one method setDate which takes the string inn the format mm/dd/yyyy.This will set date according to the input string. can somebody point me to the rite direction.any help would be deeply appreciated. Thanks Cheers! AB

      I Offline
      I Offline
      Ista
      wrote on last edited by
      #2

      Javascript baby! Ohh yeah:-D 1 line of code equals many bugs. So don't write any!!

      1 Reply Last reply
      0
      • J JohnyCoder

        hello everybody: i am new to VB.net I have to design a custom web server control which validates itself on client side and server-side. which means that i would not use a seperate custom validator for validation, but it should be a part of the custom control itself.and this validation would be done on clientside first, and incase javascript is off in browser then validation is done on server side. The description of the custom control is as follows: its a date control which has 3 dropdowns(dds).one for month, second for days and third for days.it has two properties isDOB(boolean) and getDate which returns the date in the format mm/dd/yyyy. one method setDate which takes the string inn the format mm/dd/yyyy.This will set date according to the input string. can somebody point me to the rite direction.any help would be deeply appreciated. Thanks Cheers! AB

        M Offline
        M Offline
        minhpc_bk
        wrote on last edited by
        #3

        Hi there, You may need to read the documentation in MSDN on how to develop an ASP.NET custom control, the link below should get you started: Developing ASP.NET Server Controls[^] In your case, you can start with implementing a composite custom control which basically consists of 3 dropdownlists and validators.

        J 1 Reply Last reply
        0
        • M minhpc_bk

          Hi there, You may need to read the documentation in MSDN on how to develop an ASP.NET custom control, the link below should get you started: Developing ASP.NET Server Controls[^] In your case, you can start with implementing a composite custom control which basically consists of 3 dropdownlists and validators.

          J Offline
          J Offline
          JohnyCoder
          wrote on last edited by
          #4

          HI: Thanks for reply. Actually i know how to develope a composite control.But i m just confused about the validation part.if i was to develope a seperate a validation custom server control. that was not a problem. But i dont know how to embede validation control inside inside this composite control.Should write a seperate class(inherited from base validator) and then instantiate it inside my control. i m searching the web. but mostly i m finding separate validation controls for custom control. its hard to find self validating custom server control. if u come across something like that, please inform me. Thanks AB

          M 1 Reply Last reply
          0
          • J JohnyCoder

            HI: Thanks for reply. Actually i know how to develope a composite control.But i m just confused about the validation part.if i was to develope a seperate a validation custom server control. that was not a problem. But i dont know how to embede validation control inside inside this composite control.Should write a seperate class(inherited from base validator) and then instantiate it inside my control. i m searching the web. but mostly i m finding separate validation controls for custom control. its hard to find self validating custom server control. if u come across something like that, please inform me. Thanks AB

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            aamirbabar wrote:

            But i dont know how to embede validation control inside inside this composite control.Should write a seperate class(inherited from base validator) and then instantiate it inside my control.

            That depends on your requirements when validating data, then yes, you can create your own custom validator in a seperate class inheriting from the base validator, or you can use the built-in custom validator. You then simply embed the validator in your custom composite control as you would with the dropdownlist control. Below is a quick example for a self-validating custom control which consists of a dropdownlist and a custom validator:

            \[ToolboxData("<{0}:SelfValidatingControl runat=server></{0}:SelfValidatingControl>")\]
            public class SelfValidatingControl : System.Web.UI.WebControls.WebControl, INamingContainer
            {
            	private DropDownList m\_DropDownList;
            	private CustomValidator m\_CustomValidator;
                                 
            	protected override void CreateChildControls()
            	{
            		m\_DropDownList = new DropDownList();
            		m\_DropDownList.ID = "DropDownList1";
            		m\_DropDownList.Items.Add(new ListItem("Text1", "Value1"));
            		m\_DropDownList.Items.Add(new ListItem("Text2", "Value2"));
            		m\_DropDownList.Items.Add(new ListItem("Text3", "Value3"));
            		this.Controls.Add(m\_DropDownList);
                                         
            		m\_CustomValidator = new CustomValidator();
            		m\_CustomValidator.ControlToValidate = m\_DropDownList.ID;
            		m\_CustomValidator.ClientValidationFunction = "MyClientValidationFunc";
            		m\_CustomValidator.ServerValidate +=new ServerValidateEventHandler(m\_CustomValidator\_ServerValidate);
            		this.Controls.Add(m\_CustomValidator);
                                        
            		HtmlGenericControl script = new HtmlGenericControl("script");
            		script.InnerText = "function MyClientValidationFunc(source, arguments){alert('excute my client validation');}";
            		this.Controls.Add(script);
            	}
                                
            	private void m\_CustomValidator\_ServerValidate(object source, ServerValidateEventArgs args)
            	{
            		HttpContext.Current.Response.Write("Execute my server validation function");
            	}
            }
            
            J 2 Replies Last reply
            0
            • M minhpc_bk

              aamirbabar wrote:

              But i dont know how to embede validation control inside inside this composite control.Should write a seperate class(inherited from base validator) and then instantiate it inside my control.

              That depends on your requirements when validating data, then yes, you can create your own custom validator in a seperate class inheriting from the base validator, or you can use the built-in custom validator. You then simply embed the validator in your custom composite control as you would with the dropdownlist control. Below is a quick example for a self-validating custom control which consists of a dropdownlist and a custom validator:

              \[ToolboxData("<{0}:SelfValidatingControl runat=server></{0}:SelfValidatingControl>")\]
              public class SelfValidatingControl : System.Web.UI.WebControls.WebControl, INamingContainer
              {
              	private DropDownList m\_DropDownList;
              	private CustomValidator m\_CustomValidator;
                                   
              	protected override void CreateChildControls()
              	{
              		m\_DropDownList = new DropDownList();
              		m\_DropDownList.ID = "DropDownList1";
              		m\_DropDownList.Items.Add(new ListItem("Text1", "Value1"));
              		m\_DropDownList.Items.Add(new ListItem("Text2", "Value2"));
              		m\_DropDownList.Items.Add(new ListItem("Text3", "Value3"));
              		this.Controls.Add(m\_DropDownList);
                                           
              		m\_CustomValidator = new CustomValidator();
              		m\_CustomValidator.ControlToValidate = m\_DropDownList.ID;
              		m\_CustomValidator.ClientValidationFunction = "MyClientValidationFunc";
              		m\_CustomValidator.ServerValidate +=new ServerValidateEventHandler(m\_CustomValidator\_ServerValidate);
              		this.Controls.Add(m\_CustomValidator);
                                          
              		HtmlGenericControl script = new HtmlGenericControl("script");
              		script.InnerText = "function MyClientValidationFunc(source, arguments){alert('excute my client validation');}";
              		this.Controls.Add(script);
              	}
                                  
              	private void m\_CustomValidator\_ServerValidate(object source, ServerValidateEventArgs args)
              	{
              		HttpContext.Current.Response.Write("Execute my server validation function");
              	}
              }
              
              J Offline
              J Offline
              JohnyCoder
              wrote on last edited by
              #6

              Thanks minhpc_bk: I think this would help. i m gonna try this. But one more q. i want to catch the error in summary control. would that be a problem? Thanks ab

              M 1 Reply Last reply
              0
              • M minhpc_bk

                aamirbabar wrote:

                But i dont know how to embede validation control inside inside this composite control.Should write a seperate class(inherited from base validator) and then instantiate it inside my control.

                That depends on your requirements when validating data, then yes, you can create your own custom validator in a seperate class inheriting from the base validator, or you can use the built-in custom validator. You then simply embed the validator in your custom composite control as you would with the dropdownlist control. Below is a quick example for a self-validating custom control which consists of a dropdownlist and a custom validator:

                \[ToolboxData("<{0}:SelfValidatingControl runat=server></{0}:SelfValidatingControl>")\]
                public class SelfValidatingControl : System.Web.UI.WebControls.WebControl, INamingContainer
                {
                	private DropDownList m\_DropDownList;
                	private CustomValidator m\_CustomValidator;
                                     
                	protected override void CreateChildControls()
                	{
                		m\_DropDownList = new DropDownList();
                		m\_DropDownList.ID = "DropDownList1";
                		m\_DropDownList.Items.Add(new ListItem("Text1", "Value1"));
                		m\_DropDownList.Items.Add(new ListItem("Text2", "Value2"));
                		m\_DropDownList.Items.Add(new ListItem("Text3", "Value3"));
                		this.Controls.Add(m\_DropDownList);
                                             
                		m\_CustomValidator = new CustomValidator();
                		m\_CustomValidator.ControlToValidate = m\_DropDownList.ID;
                		m\_CustomValidator.ClientValidationFunction = "MyClientValidationFunc";
                		m\_CustomValidator.ServerValidate +=new ServerValidateEventHandler(m\_CustomValidator\_ServerValidate);
                		this.Controls.Add(m\_CustomValidator);
                                            
                		HtmlGenericControl script = new HtmlGenericControl("script");
                		script.InnerText = "function MyClientValidationFunc(source, arguments){alert('excute my client validation');}";
                		this.Controls.Add(script);
                	}
                                    
                	private void m\_CustomValidator\_ServerValidate(object source, ServerValidateEventArgs args)
                	{
                		HttpContext.Current.Response.Write("Execute my server validation function");
                	}
                }
                
                J Offline
                J Offline
                JohnyCoder
                wrote on last edited by
                #7

                Hi: I just realized that i have three dropdowns mm/dd/yyyy.i can not pass the id of one dropdown. I have to pass the value in the form mm/dd/yyy. so how can i use controlToValidate property of the validator.? Help

                M 1 Reply Last reply
                0
                • J JohnyCoder

                  Thanks minhpc_bk: I think this would help. i m gonna try this. But one more q. i want to catch the error in summary control. would that be a problem? Thanks ab

                  M Offline
                  M Offline
                  minhpc_bk
                  wrote on last edited by
                  #8

                  Hi there, I'm quite busy on these days so I cannot reply to you as soon as you might expect, sorry about this. There would not be a problem when you use a summary control to display the error message.

                  1 Reply Last reply
                  0
                  • J JohnyCoder

                    Hi: I just realized that i have three dropdowns mm/dd/yyyy.i can not pass the id of one dropdown. I have to pass the value in the form mm/dd/yyy. so how can i use controlToValidate property of the validator.? Help

                    M Offline
                    M Offline
                    minhpc_bk
                    wrote on last edited by
                    #9

                    There are two things that you might need to know: + There are a couple of ways to get the value of the dropdownlist at the client side. One of them is to use the document.getElementById method which takes the clientid of the dropdownlist as the method parameter. + The ControlToValidate property is not required for the CustomValidator (though it is for other validators), so you may not need to specify the value for this property. What you might need to do is to emit the ClientID of the 3 dropdownlists in your custom validation function so that you can use at the client side to get the value from the dropdownlists. The sample code looks like:

                    script.InnerText = "function MyClientValidationFunc(source, arguments){var ddlDay = document.getElementById('" + this.m_DropDownList.ClientID + "'); ... }";

                    The emitted script will contain the following piece of script:

                    ...
                    var ddlDay = document.getElementById('...');
                    //Now you can easily get the value of the dropdownlist using the value property.
                    var day = ddlDay.value;
                    ....

                    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