Q: Textbox press Enter submit form (ASCX version)

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Q: Textbox press Enter submit form (ASCX version)

    Hello all,

    It's known to how to make user press enter
    in an asp:textbox and the form is submited by
    adding an attribute to the textbox with an
    onkeydown jscript eventhandler.

    For example,
    -----------------------------------------
    <asp:textbox id="t1" runat="server"/>
    <asp:button id="b1" runat="server"/>

    page_load event:
    t1.Attributes.Add("onkeypress", "submitForm();");

    Client-side Script:
    function submitForm()
    {
    if (event.keyCode == 13)
    {
    event.cancelBubble = true;
    event.returnValue = false;
    document.all.b1.click();
    }
    }
    -----------------------------------------

    The above should work well when used inside an
    ASPX page.
    However, my situation now is the thing happens
    inside an ASCX, a dynamically loaded user
    control.
    As u all know, the actual ID of the asp:button
    varies when the control is rendered.
    It could be

    parentCtrl__ctl0_b1

    or something else.

    So the trick in the client side script has to
    be modified. But how to? And any other more
    flexible ways? Somebody plz give me some hints!

    Jordan


    w. jORDAN Guest

  2. Similar Questions and Discussions

    1. Enter key causes submit action in form
      I've created a form using Dreamweaver 4.0 and I see that pressing the Enter key in any blank field in the form causes a "Submit" action. When there...
    2. cf flash form submit on enter key down?
      Hey all, How can i make a flash form submit upon the user hitting the ENTER key? I would mostly like it to just be when either the last field is...
    3. disable enter button to submit form
      I'm very new to ColdFusion - I'm building forms in Deamweaver, then using Cld fusion to pass he data to an Access Database. So far, so good. But...
    4. Textbox and button -- enter key/submit - REFERENCE
      Hi, as you can see from my many posts the past few days, I have certainly been confused about the firing/non-firing of my textboxes and buttons. In...
    5. why datagrid's itemcommand event fired when I press ENTER key in a TextBox
      Hi, When you press the ENTER key the default button pressed and since the only button is the column template one a post back to the server...
  3. #2

    Default Re: Q: Textbox press Enter submit form (ASCX version)

    YES, your advice does help!

    Except that
    t1.Attributes.Add("onkeypress", "submitForm(" + b1.ClientId + ");");
    should be
    t1.Attributes.Add("onkeypress", "submitForm(\"" + b1.ClientID +
    "\");");

    And
    document.getElementById(submitButtonId).click();
    should be
    document.all[submitButtonId].click(); // for better browser
    compatibility

    OK, my thing has been done now.
    Thank you very much for your quick help!

    Best Regards,
    Jordan

    "Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
    news:Xns93BF1C9CDC8BAcrtimmonscrtimmonsin@207.46.2 48.16...
    > Jordan,
    >
    > Pass the submit button's ClientId as a parameter to the submitForm
    > function (untested):
    >
    >
    > page_load event:
    > t1.Attributes.Add("onkeypress", "submitForm(" + b1.ClientId + ");");
    >
    > Client-side Script:
    > function submitForm(submitButtonId)
    > {
    > if (event.keyCode == 13)
    > {
    > event.cancelBubble = true;
    > event.returnValue = false;
    > document.getElementById(submitButtonId).click();
    > }
    > }
    >
    >
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > [url]http://www.crtimmonsinc.com/[/url]

    w. jORDAN Guest

  4. #3

    Default Re: Q: Textbox press Enter submit form (ASCX version)

    Hi
    > document.all[submitButtonId].click(); // for better browser
    > compatibility
    Wrong "document.all" only works on Explorer

    For better browser compatibility
    document.forms[0].submitBtn.click();

    --
    Best Regards
    Vidar Petursson
    ==============================
    Microsoft Internet Client & Controls MVP
    ==============================
    "w. jORDAN" <wmjordan@163.com> wrote in message
    news:%23nXRVM2TDHA.2204@TK2MSFTNGP12.phx.gbl...
    > YES, your advice does help!
    >
    > Except that
    > t1.Attributes.Add("onkeypress", "submitForm(" + b1.ClientId + ");");
    > should be
    > t1.Attributes.Add("onkeypress", "submitForm(\"" + b1.ClientID +
    > "\");");
    >
    > And
    > document.getElementById(submitButtonId).click();
    > should be
    > document.all[submitButtonId].click(); // for better browser
    > compatibility
    >
    > OK, my thing has been done now.
    > Thank you very much for your quick help!
    >
    > Best Regards,
    > Jordan
    >
    > "Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
    > news:Xns93BF1C9CDC8BAcrtimmonscrtimmonsin@207.46.2 48.16...
    > > Jordan,
    > >
    > > Pass the submit button's ClientId as a parameter to the submitForm
    > > function (untested):
    > >
    > >
    > > page_load event:
    > > t1.Attributes.Add("onkeypress", "submitForm(" + b1.ClientId + ");");
    > >
    > > Client-side Script:
    > > function submitForm(submitButtonId)
    > > {
    > > if (event.keyCode == 13)
    > > {
    > > event.cancelBubble = true;
    > > event.returnValue = false;
    > > document.getElementById(submitButtonId).click();
    > > }
    > > }
    > >
    > >
    > > Hope this helps.
    > >
    > > Chris.
    > > -------------
    > > C.R. Timmons Consulting, Inc.
    > > [url]http://www.crtimmonsinc.com/[/url]
    >
    >

    Vidar Petursson Guest

  5. #4

    Default Re: Q: Textbox press Enter submit form (ASCX version)

    Hi Vidar,

    Yes, i know that document.all only works on IE and Opera 7.

    But submitButtonId is a string,
    can it be used like that?

    document.forms[0].submitButtonId.click();

    Jordan

    "Vidar Petursson" <theking@icysoft.com> wrote in message
    news:eZ7RVM4TDHA.2188@TK2MSFTNGP10.phx.gbl...
    > Hi
    >
    > > document.all[submitButtonId].click(); // for better browser
    > > compatibility
    > Wrong "document.all" only works on Explorer
    >
    > For better browser compatibility
    > document.forms[0].submitBtn.click();
    >
    > --
    > Best Regards
    > Vidar Petursson
    > ==============================
    > Microsoft Internet Client & Controls MVP
    > ==============================
    > "w. jORDAN" <wmjordan@163.com> wrote in message
    > news:%23nXRVM2TDHA.2204@TK2MSFTNGP12.phx.gbl...
    > > YES, your advice does help!
    > >
    > > Except that
    > > t1.Attributes.Add("onkeypress", "submitForm(" + b1.ClientId +
    ");");
    > > should be
    > > t1.Attributes.Add("onkeypress", "submitForm(\"" + b1.ClientID +
    > > "\");");
    > >
    > > And
    > > document.getElementById(submitButtonId).click();
    > > should be
    > > document.all[submitButtonId].click(); // for better browser
    > > compatibility
    > >
    > > OK, my thing has been done now.
    > > Thank you very much for your quick help!
    > >
    > > Best Regards,
    > > Jordan
    > >
    > > "Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in
    message
    > > news:Xns93BF1C9CDC8BAcrtimmonscrtimmonsin@207.46.2 48.16...
    > > > Jordan,
    > > >
    > > > Pass the submit button's ClientId as a parameter to the submitForm
    > > > function (untested):
    > > >
    > > >
    > > > page_load event:
    > > > t1.Attributes.Add("onkeypress", "submitForm(" + b1.ClientId + ");");
    > > >
    > > > Client-side Script:
    > > > function submitForm(submitButtonId)
    > > > {
    > > > if (event.keyCode == 13)
    > > > {
    > > > event.cancelBubble = true;
    > > > event.returnValue = false;
    > > > document.getElementById(submitButtonId).click();
    > > > }
    > > > }
    > > >
    > > >
    > > > Hope this helps.
    > > >
    > > > Chris.
    > > > -------------
    > > > C.R. Timmons Consulting, Inc.
    > > > [url]http://www.crtimmonsinc.com/[/url]
    > >
    > >
    >
    >

    w. jORDAN Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139