Results 1 to 10 of 10

Thread: web form submit behavior in ASP.NET RESOLVED

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2003
    Location
    NYC
    Posts
    50

    web form submit behavior in ASP.NET RESOLVED

    How can I stop or catch or detect when a user hits enter in my web form. I have all of the database input code inside a submit button and if the user hits enter the form submits, is cleared and missed all of my code. Also the user hates the form submit on enter behavior.

    Is there some way to detect an event argument or something in the page unload event (or some event) to check if it was triggered by enter and if so then go back? What is the asp.net code equivalent of the user hitting the back key?

    I am hoping I do not need a client side javascript here because it seems like a lot of work... and I don't know javascript.

    Thanks.
    Last edited by soskinny; Aug 6th, 2004 at 09:43 AM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    ASP.NET is server-side technology. You can't stop the default submit on Enter action of many browsers,
    without including client-side technology......... javascript.

    Now, even though ASP.NET is server-side technology, you can still use it to emit client-side code (javascript)
    into the response stream to the client.

    The javascript to block a Enter is:

    Code:
    function blockEnter(evt)
    {
     
     if (evt.keyCode == 13)
     {
     
        if (evt.srcElement.tagName=="TEXTAREA")
          return true;
        return false;
      }
    }
    Now, to call a javascript function when a key is pressed on a textbox, you add this line to code-behind page.

    VB Code:
    1. Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
    2.  
    3. TextBox1.Attributes.Add("onKeyDown", "return blockEnter(event)")


    This will render the textbox with a javascript 'onKeyDown' event on the client-side.

    One more thing we have to do is get javascript code to the page. You can either type it directly into the aspx front side,
    or add the following lines to the Page_PreRender function...
    VB Code:
    1. Dim myjavascriptstring As String = _
    2. "<script language=""javascript"">function blockEnter(evt){if (evt.keyCode == 13){if (evt.srcElement.tagName==""TEXTAREA"")return true;return false;}}</script>"
    3.  
    4. Me.RegisterClientScriptBlock("c12323",myjavascriptstring)


    Now, additionally, you should check to see if the TextBox is empty when the user clicks Submit, and not replace the text (instead offer a Clear button).
    Last edited by nemaroller; Aug 4th, 2004 at 10:00 AM.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2003
    Location
    NYC
    Posts
    50
    Thanks Nemaroller- you are a life saver. This looks perfect and do-able. I'll implement it. And buy Javascript for dummies! I can't ignore the performance of client side script any longer...

    I'm still surprised that I can't simply detect the key press on the server side and then cancel the submit. I know it's not good practice and creates an extra round trip, though.

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    soskinny
    Did this work the way you wanted, I need to do a similar thing and would like to know.


    Things I do when I am bored: DotNetable

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2003
    Location
    NYC
    Posts
    50
    Yes, this worked perfectly and was very easy to implement. It only took a few minutes to add to my page. The form doesn't react at all to the enter key being pressed while in a text box.

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2003
    Location
    NYC
    Posts
    50
    am I supposed to tag this thread solved? or indicate success? if so, someone please let me know how...

  7. #7
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    You can edit your original post and change the subject field. Add the word RESOLVED to the end to let people see at a glance the question is solved.

    Thanks


    Things I do when I am bored: DotNetable

  8. #8
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: web form submit behavior in ASP.NET RESOLVED

    I have a text box and a submit button. How do I capture the user hitting enter while the text box has focus and calling the submit button's click event?

    I don't want to put the button's click event code in the Page_Load event as it causes other problems. Alternatively, I could put the button's click event code in the Page_Load event if I was able to detect which object/event caused the page to be submitted to the server.

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2003
    Location
    NYC
    Posts
    50

    Re: web form submit behavior in ASP.NET RESOLVED

    Robertx-
    you should scroll up and review nemaroller's advice. it worked perfectly for me. unless I misunderstand your question, that should do the trick.

  10. #10
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: web form submit behavior in ASP.NET RESOLVED

    Thanks, soskinny. I used an adaptation of this code from another thread:
    VB Code:
    1. Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
    2.         txtSearch.Attributes.Add("onkeydown", String.Format("if(window.event.keyCode == 13) {{{0}.focus();}}", cmdSearch.ClientID))
    3.     End Sub
    and it did exactly what I wanted it to do. ie.calls the cmdSearch button click event when the user hits enter when txtSearch has focus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width