Q: Textbox press Enter submit form (ASCX version)

Discussion in 'ASP .Net' started by w. jORDAN, Jul 21, 2003.

  1. w. jORDAN

    w. jORDAN Guest

    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, Jul 21, 2003
    #1
    1. Advertising

  2. w. jORDAN

    w. jORDAN Guest

    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.248.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.
    > http://www.crtimmonsinc.com/
    w. jORDAN, Jul 21, 2003
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:%...
    > 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.248.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.
    > > http://www.crtimmonsinc.com/

    >
    >
    Vidar Petursson, Jul 21, 2003
    #3
  4. w. jORDAN

    w. jORDAN Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%...
    > > 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.248.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.
    > > > http://www.crtimmonsinc.com/

    > >
    > >

    >
    >
    w. jORDAN, Jul 22, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur
    Replies:
    0
    Views:
    572
    Natty Gur
    Jun 30, 2003
  2. Chad Z. Hower aka Kudzu
    Replies:
    1
    Views:
    660
  3. Andrea Williams

    Key press "Enter" does not submit the form

    Andrea Williams, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    7,401
    Steve C. Orr [MVP, MCSD]
    May 24, 2004
  4. Fabio Cavassini

    Disable Form Submit when I press enter

    Fabio Cavassini, Nov 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    5,180
    Kevin Spencer
    Nov 29, 2005
  5. WJ
    Replies:
    1
    Views:
    3,876
    Adrienne
    Jan 19, 2005
Loading...

Share This Page