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. Selective Form Validation Using ASP.NET Validation Controls

Selective Form Validation Using ASP.NET Validation Controls

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netwinforms
5 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.
  • A Offline
    A Offline
    Ahmed Gaser
    wrote on last edited by
    #1

    Dear all i have a problem in a web form ,it contains two user controls ,each control has its own validation controls ,the problem is ,when I put the two user controls in the same page ,when I push a button in one of them the other validation controls in the other user controls fires even if the user control where I pushed the button is valid ,so please if any one knows an article opr a sample that discuss this issue please inform me Thanks

    M 1 Reply Last reply
    0
    • A Ahmed Gaser

      Dear all i have a problem in a web form ,it contains two user controls ,each control has its own validation controls ,the problem is ,when I put the two user controls in the same page ,when I push a button in one of them the other validation controls in the other user controls fires even if the user control where I pushed the button is valid ,so please if any one knows an article opr a sample that discuss this issue please inform me Thanks

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

      Hi there Because you place the two controls in the same page, then at the client side they all are placed in a form element. So when you click a button, all validators on the form will be running. In this case, if you want to seperate validators, you are required to provide a bit more work. What you need to do is to keep the validators of each user control in an array, it's the same as ASP.NET emits the client-side scripts to hold all validators:

      var Page_Validators = new Array(document.all["RequiredFieldValidator1"],document.all["RequiredFieldValidator2"]);

      At the client side, depending on which button clicked by the user, you can enable/disable the validators of each user control by getting through the validator list and setting the enabled property of each invidual validator. To emit the client-side scripts in the page, you can take a quick look at the methods of the Page class. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuipagememberstopic.asp[^]

      A 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there Because you place the two controls in the same page, then at the client side they all are placed in a form element. So when you click a button, all validators on the form will be running. In this case, if you want to seperate validators, you are required to provide a bit more work. What you need to do is to keep the validators of each user control in an array, it's the same as ASP.NET emits the client-side scripts to hold all validators:

        var Page_Validators = new Array(document.all["RequiredFieldValidator1"],document.all["RequiredFieldValidator2"]);

        At the client side, depending on which button clicked by the user, you can enable/disable the validators of each user control by getting through the validator list and setting the enabled property of each invidual validator. To emit the client-side scripts in the page, you can take a quick look at the methods of the Page class. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuipagememberstopic.asp[^]

        A Offline
        A Offline
        Ahmed Gaser
        wrote on last edited by
        #3

        I Already disabled it in the client side ,but I can't disable it at the server side ,when I click on the button in one control ,it fires the other validation controls at the other user control (after running the server side script under this butoon),!! ,so what can I do ?

        M 1 Reply Last reply
        0
        • A Ahmed Gaser

          I Already disabled it in the client side ,but I can't disable it at the server side ,when I click on the button in one control ,it fires the other validation controls at the other user control (after running the server side script under this butoon),!! ,so what can I do ?

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

          Hi there, In this case, you need a way to determine which user control is submitted, then in the code behind you can enable/disable the validators of each control. For example, I use a hidden element in a user control, then depending on which button is clicked at the client side, I am going to set a value for the right hidden element. The sample code in the code behind of a user control looks something like this:

          private void Page_Load(object sender, System.EventArgs e)
          {
          ...
          if(Page.IsPostBack)
          {
          string submitType = Request[this.hiddenElement.Name];
          if(submitType=="")
          {
          EnableValidators(false);
          }
          }
          ...
          }

          private void Page_PreRender(object sender, System.EventArgs e)
          {
          //By default, all validators are enabled.
          EnableValidators(true);
          }

          In the EnableValidators() method, you can enable/disable the validators based on the method parameter.

          A 1 Reply Last reply
          0
          • M minhpc_bk

            Hi there, In this case, you need a way to determine which user control is submitted, then in the code behind you can enable/disable the validators of each control. For example, I use a hidden element in a user control, then depending on which button is clicked at the client side, I am going to set a value for the right hidden element. The sample code in the code behind of a user control looks something like this:

            private void Page_Load(object sender, System.EventArgs e)
            {
            ...
            if(Page.IsPostBack)
            {
            string submitType = Request[this.hiddenElement.Name];
            if(submitType=="")
            {
            EnableValidators(false);
            }
            }
            ...
            }

            private void Page_PreRender(object sender, System.EventArgs e)
            {
            //By default, all validators are enabled.
            EnableValidators(true);
            }

            In the EnableValidators() method, you can enable/disable the validators based on the method parameter.

            A Offline
            A Offline
            Ahmed Gaser
            wrote on last edited by
            #5

            sorry,can you please write more code for the client side and the server side,because it's so difficult to me to handle it thanks

            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