Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Q: Textbox press Enter submit form (ASCX version)

Reply
Thread Tools

Q: Textbox press Enter submit form (ASCX version)

 
 
w. jORDAN
Guest
Posts: n/a
 
      07-21-2003
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


 
Reply With Quote
 
 
 
 
w. jORDAN
Guest
Posts: n/a
 
      07-21-2003
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.
> http://www.crtimmonsinc.com/



 
Reply With Quote
 
 
 
 
Vidar Petursson
Guest
Posts: n/a
 
      07-21-2003
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
> > http://www.crtimmonsinc.com/

>
>



 
Reply With Quote
 
w. jORDAN
Guest
Posts: n/a
 
      07-22-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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.
> > > http://www.crtimmonsinc.com/

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to know when an enter key press is used to select an item in thebrowser's context dropdown and not to submit a form? Daniel Smedegaard Buus Javascript 4 08-07-2008 04:18 PM
Disable Form Submit when I press enter Fabio Cavassini ASP .Net 1 11-29-2005 09:04 PM
IE 5+ default form submit on Enter Key Press WJ HTML 1 01-19-2005 08:27 AM
Form submit - hitting enter does not trigger Submit button ASP General 2 10-25-2004 03:37 PM
Key press "Enter" does not submit the form Andrea Williams ASP .Net 1 05-24-2004 09:40 PM



Advertisments