User control reloads on Submit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cosmos411
    New Member
    • Sep 2008
    • 6

    User control reloads on Submit

    Hi everyone,

    I have a captcha type of user control that's on a contact page. The problem is that when the user hits Submit it reloads the user control and changes the captcha inside the control before I have a chance to validate against it. This causes the validation to always fail. I was wondering why this happens and what I can do about it. Sorry for being so vague, but this is the best way I can describe the problem.

    Thanks to anyone who can help.

    Gord
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Gord,

    I don't know what you mean by "captcha" - could you explain?

    You can use javascript to validate your controls by firing a function on the forms OnSubmit event like this.

    Code:
    <script type='text/javascript'>
    function ValidateControls()
    {
    // your validation code goes here
    }
    </script>
    <form name='f1' action='page.asp' OnSubmit='return ValidateControls();'>
    <!-- your controls -->
    </form>
    Have you ever used javascript before?

    Dr B

    Comment

    • cosmos411
      New Member
      • Sep 2008
      • 6

      #3
      Hey Dr B,

      Thanks for your reply. That's true, this could work, but I was hoping for something with code behind.

      The term "captcha" means something a user has to do to show they're human. Whenever you're asked to enter the squiggly word you see on forums, etc., that's a captcha. My captcha is simple. It simply reproduce an image a random number of times. The user is to enter the number of times the image appears in order to submit the contact page. The problem is when the user clicks Submit on the contact page, the captcha page reloads, thus regerating the images before I can check if the user entered the correct number.

      Any other idea?

      Thanks everyone.

      Gord

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        Gord,

        I have a doubt from something you said. "Classic" ASP handles this type of thing very different from ASP.NET. Are you using ASP.NET (.aspx file extension) or "classic" ASP (.asp file extension)?

        Jared

        Comment

        • cosmos411
          New Member
          • Sep 2008
          • 6

          #5
          These are for aspx pages. I'm relatively new to ASP.NET (< year) so the whole sequence of events is still newish to me. I'm from a desktop application background which makes more sense to me. Still getting the hang of this.

          Thanks for taking the time to read and reply!

          G

          Comment

          • jhardman
            Recognized Expert Specialist
            • Jan 2007
            • 3405

            #6
            Maybe it would make sense then to outline the sequence of events.

            1- The form (including the captcha and the textbox the user uses to enter the text) is displayed.

            2- The user fills in the blanks

            3- The user submits the form to the server

            4- The server handles the form - this is where the validation should occur

            5- The server returns a response to the user - either a "thank you for submitting" message and continue on to the next page, or a "try again" message or whatever.

            My guess is the captcha is being generated at the wrong point, it should really only appear in step 1 (or step 5 in the case of a do-over). It sounds like it is being called when the form is submitted. Does this make sense?

            Jared

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Are you checking for IsPostBack in the page_load() event? It will be false on a fresh page load, but will be true when a user clicks an ASPX button(or other control) that calls a "postback".

              Comment

              • cosmos411
                New Member
                • Sep 2008
                • 6

                #8
                First of all I have to thank everyone for their comments!

                The sequence of events mentioned above was correct. The captcha user control is on the contact page. I don't call it anywhere to load in my code, it just loads on its own (obviously I guess). I added code to the submit button that is on the contact page. The click event basically does the following:

                void btnSubmit_Click (Object sender, CommandEventArg s e)
                {

                if (mycaptcha.Vali dates())
                {
                //continue with processing the contact page and sending email etc
                }
                else
                {
                maycaptcha.Erro rLabel.visible = true;
                }
                }

                The problem is that the captcha control Page_Load fires before the code in my submit click event is hit, even though it's the very first line of code.

                I have tried a Page.IsPostback inside my user control but that causes problems. If, in the Page_Load for my user control I do

                if (!Page.IsPostba ck)
                {
                GenerateImages( );
                }
                else
                {
                //do nothing
                }

                then the captcha shows up empty on a postback. I want it to generate a new captcha no matter what, I just want to be able to check it's values before the user control fires it Page_Load.

                Sorry for the long message. Hopefully I haven't annoyed everyone.

                Thanks in advance for all your time.

                Gord

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  In the submit button code add the generateImage() line at the end if need be?

                  Comment

                  • cosmos411
                    New Member
                    • Sep 2008
                    • 6

                    #10
                    I was hoping to keep the captcha control self contained, but this could work. Thanks for the input, I'm going to give it a try!

                    Comment

                    Working...