Make button "default"


P

Paul Turley

Does anyone have some sample script to "press" a button or image button when
the user presses the Enter key? I see this behavior on a lot of web pages
but I'm not finding any samples.

Thx
 
Ad

Advertisements

J

Jos Branders

Paul Turley said:
Does anyone have some sample script to "press" a button or image button when
the user presses the Enter key? I see this behavior on a lot of web pages
but I'm not finding any samples.

Thx

Just use the HTML tag <input type="submit" value="click here or press
enter">
 
F

Felix Wu

Hi Paul,

In case you have multiple buttons on a webform, you could try this method.

When a webform contains multiple buttons, the first one rendered will be
fired when you press the Enter button. However, we can workaround this
problem using the
following method:

1. Add the following script code to the <head> section of the aspx page.

<SCRIPT LANGUAGE="javascript">
function testEnterKey()
{
if (event.keyCode == 13)
{
event.cancelBubble = true;
event.returnValue = false;
Form1.Button2.click();
}
}
</SCRIPT>

2. In the code-behind page, add the onkeypress event handler to the
attributes collectioins of the TextBox(es) on the page:

private void Page_Load(object sender, System.EventArgs e)
{
//other initialization code

TextBox1.Attributes.Add("onkeypress", "testEnterKey();");
TextBox2.Attributes.Add("onkeypress", "testEnterKey();");
......
}

More information:

HOWTO: Prevent Form Submission When User Presses the ENTER Key on a Form
(Q298498)
http://support.microsoft.com/support/kb/articles/Q298/4/98.ASP

returnValue Property
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/returnv
alue_1.asp

cancelBubble Property
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cancelb
ubble.asp

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
| From: "Paul Turley" <[email protected]>
| Subject: Make button "default"
| Date: Wed, 25 Jun 2003 22:08:00 -0700
| Lines: 12
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <[email protected]>
| Newsgroups:
microsoft.public.dotnet.framework.aspnet.webcontrols,microsoft.public.dotnet
.framework.aspnet
| NNTP-Posting-Host: ts46-03-qdr3731.porch.wa.charter.com 68.113.26.153
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet:155003
microsoft.public.dotnet.framework.aspnet.webcontrols:12621
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Does anyone have some sample script to "press" a button or image button
when
| the user presses the Enter key? I see this behavior on a lot of web pages
| but I'm not finding any samples.
|
| Thx
|
| --
| Paul Turley, MCSD, MCAD, MCT, MSF Practitioner, A+ Technician
| (e-mail address removed)
|
|
|
|
 
P

Paul Turley

I've tried this. I'm setting the tab index of my web server button to 1 and
I've set it to the next index in sequence for all the controls. In either
case, pressing Enter does nothing. This is on a web user control. I think
I need to use client-side script to capture the Enter key press - unless I'm
missing something.
 
P

Paul Turley

The web server button control renders these tags: <input type="submit"
name="Login_Control1:btnLogin" ...

I am calling server-side code on the click event so I need to use a web
server button or image button.

I've set the tab index to 0, 1 and other values. Any ideas?
 
Ad

Advertisements

J

Jurij P

I used this script for component asp:button but it didn't work. Than I
changed the component with asp:imagebutton (also added gif image for button)
and used this script. Now it works right.

<input type="text" onkeypress="if (event.keyCode == 13) return false"
runat="server">

lep pozdrav, Jure
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top