Validation in Ajax updatepanel - not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user1980
    New Member
    • Dec 2009
    • 112

    Validation in Ajax updatepanel - not working

    Hello there...

    can somebody guide me on this…

    I have a panel (say, panel1) inside another panel (say, outer-panel)….The panel1 is visible only when a checkbox is checked in the outer-panel. I am using ajax update panel to do this.

    Now I have many text boxes in the panel1. I have to put a validation on all the boxes.

    I tried to test this by putting validation on 2 boxes and this works fine on my local machine. When I put the same logic on the server, it does not. Of course the page on my local machine has fewer text boxes compared to the page on server. But I do not understand why the same logic works on my local machine and does not work on the server. I did try to put a validation group, but of no use....

    The page on the sever is a very long one and also it has a captcha control….. when ever I try not to enter text in one the text boxes which has validation and then hit submit…it redirects me to another page with out prompting for the validation error.

    Can somebody please tell me what could be the reason…thanks in advance….
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Did you register your validation JavaScript with the ScriptManager?

    If you didn't you're going to run into problems where the JavaScript validation will execute as long as no asynchronous postback has been made to the server....after an asynchronous postback has happened then the previously working JavaScript will no longer work. That is one of the reasons why the ScriptManager exists, so that you can register your JavaScript in order for it to be sent to every time the page is asynchronously loaded to keep scripts working.

    -Frinny

    Comment

    • user1980
      New Member
      • Dec 2009
      • 112

      #3
      thank you for your response..I did register the script with the scriptmanager.. .but still I have this problem...

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        How did you register the script?
        (Please post the code that registers the script with the ScriptManager)

        -Frinny

        Comment

        • user1980
          New Member
          • Dec 2009
          • 112

          #5
          I am just using,
          <asp:ScriptMana ger ID="ScriptManag er1" runat="server" />

          And then using the udpatepanel to display the panel conditionally.. ....

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            That is the declaration of the ScriptManager.. ..
            Did you actually register the validation script with ScriptManager??

            See the ScriptManager class for a list of the ScriptManager Methods available to you.

            -Frinny

            Comment

            • user1980
              New Member
              • Dec 2009
              • 112

              #7
              I have many textboxes and validations fot eh text boxes in my updatepanel ContentTempalte ..so do I have to register all of them thought the script manager...I have seen the example in the page http://msdn.microsoft.com/en-us/libr...ptmanager.aspx.but I am not sure how would i register my components....s o can you please let me know if there is any other reference that I can look into....thanks you for your time...

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                This really depends on how you are doing your validation.

                I'm not sure how you are calling or using the JavaScript.
                If you have implemented some JavaScript that does the validation, and you have your button's client onclick event call this JavaScript, then all you have to do is register that script with the ScriptManager.

                -Frinny

                Comment

                • user1980
                  New Member
                  • Dec 2009
                  • 112

                  #9
                  Hi Frinny

                  I am not suing any additional javascript. For all the textboxes in the panel, I am enabling the validations in pageload...as,
                  Code:
                      protected void Page_Load(object sender, EventArgs e)
                          {
                            
                              if (gpa.Text != "")
                              {
                                  
                                  required_classgpa.Enabled = true;
                                  required_classgpa.SetFocusOnError = true;
                                
                  
                              }
                              else
                              {
                                  required_classgpa.Enabled = false;
                              }
                              if (classgpa.Text != "")
                              {
                                  required_gpa.Enabled = true;
                                  SetFocus(gpa);
                              }
                              else
                              {
                                  required_gpa.Enabled = false;
                              }
                  
                              if (classrank.Text != "")
                              {
                                  required_totalclass.Enabled = true;
                              }
                              else
                              {
                                  required_totalclass.Enabled = false;
                              }
                  
                              if (totalclass.Text != "")
                              {
                                  required_classrank.Enabled = true;
                              }
                              else
                              {
                                  required_classrank.Enabled = false;
                              }
                  
                             
                          }
                  ...
                  so in my case do I still have to register the script, then how would I register ??

                  I have tried replicating a similar scenario with fewer elements and that page works fine with out any issues....I am clueless on why this is happening...
                  Last edited by Frinavale; Mar 17 '10, 06:25 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    You should have mentioned before that you are not using JavaScript validation. That you are doing your validation on the server in your C# code.

                    The only thing that I can think of is if the elements that you are trying to work with are not inside the UpdatePanel that performed the asynchronous postback.

                    If you have a label in UpdatePanelA and a TextBox in UpdatePanelB. If UpdatePanelB performs the asynchronous postback you cannot access the label in UpdatePanelA if UpdatePanelA was not inside UpdatePanelB.

                    You can access the label in your C# code during the asynchronous postback but it will not be updated when the page is redrawn in the browser since ASP.NET strips down the response to only include the elements that were part of the asynchronous postback.

                    Does that make sense?

                    Comment

                    • user1980
                      New Member
                      • Dec 2009
                      • 112

                      #11
                      thank you for the response......i have label and text boxes in the same panel...so that could not be the reason.

                      You can access the label in your C# code during the asynchronous postback but it will not be updated when the page is redrawn in the browser since ASP.NET strips down the request to only include the elements that were part of the asynchronous postback.
                      ...this could be the possible reason...
                      but still one thing is not clear...as mentioned earlier, I did try to replicate a test page with 2 update panels and similar validations and they work fine...just when the no:of elements in the page increase, the validations fail...

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        I'm not sure what to suggest....

                        Double check your logic for the case when you add more elements.

                        -Frinny

                        Comment

                        • user1980
                          New Member
                          • Dec 2009
                          • 112

                          #13
                          thank you for all the help.. I will look into logic...

                          Comment

                          Working...