Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Simulate button click on Return key press

Reply
Thread Tools

Simulate button click on Return key press

 
 
Dan
Guest
Posts: n/a
 
      02-06-2004
Consider a simple form with a textbox and button.

Is there a way to handle the common situation where a user presses "return"
rather than clicks on the button.

I can already detect the "return" key press using the textbox's onkeypress
event and now what I'm looking for is something like:

onkeypress="if 'return' key pressed, then button.click=True"

Any ideas?





 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      02-06-2004
Maybe I should try searching for Enter key first....



You can intercept the client side enter keypress event of the text box and
then click do what you want using javascript code.
Here's a good example:
http://www.kamp-hansen.dk/pages/show...d=21&menuid=18

Or you might try using this free control.
http://www.metabuilders.com/tools/DefaultButtons.aspx

And here's a good article on the subject:
http://www.allasp.net/enterkey.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com



"Abhram Jose" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I have written an ASP.Net application.
> It has several edit box to enter data. How to program in such way that

when
> the user hits enter key focus will go to next edit box (required control )
> and so on ?
> Thanks for your suggestions.
> Abhram Jose
>
>


"Dan" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Consider a simple form with a textbox and button.
>
> Is there a way to handle the common situation where a user presses

"return"
> rather than clicks on the button.
>
> I can already detect the "return" key press using the textbox's onkeypress
> event and now what I'm looking for is something like:
>
> onkeypress="if 'return' key pressed, then button.click=True"
>
> Any ideas?
>
>
>
>
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?QWRyaWphbiBKb3NpYw==?=
Guest
Posts: n/a
 
      02-06-2004
onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()

or in other words just call the buttons click() method.

but I have no idea how to get the buttons id as the .NET framework changes all ids do something like _ctl1$_ctl4$someButton... naturally you can viewsource your page and see what id it generates and use that but that kinda hardcodes and if you change your control tree layout it might stop working.
im sure there's a way to get around this though...
 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      02-06-2004
Thanks for this.

"Adrijan Josic" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> onkeypress="if(event.keyCode==13)

document.getElementById('someButton').click()"
>
> or in other words just call the buttons click() method..
>
> but I have no idea how to get the buttons id as the .NET framework changes

all ids do something like _ctl1$_ctl4$someButton... naturally you can
viewsource your page and see what id it generates and use that but that
kinda hardcodes and if you change your control tree layout it might stop
working..
> im sure there's a way to get around this though...



 
Reply With Quote
 
xanoxate xanoxate is offline
Junior Member
Join Date: Oct 2006
Posts: 1
 
      10-19-2006
With asp.net 2.0 you can enlcose the text box and button inside a panel and then set the DefaultButton property of the panel to the id of the button, when the user presses the enter key the button will be pressed
 
Reply With Quote
 
Moim Moim is offline
Junior Member
Join Date: Jun 2007
Location: Dhaka
Posts: 2
 
      06-06-2007
Quote:
Originally Posted by =?Utf-8?B?QWRyaWphbiBKb3NpYw==?=
onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()

or in other words just call the buttons click() method.

but I have no idea how to get the buttons id as the .NET framework changes all ids do something like _ctl1$_ctl4$someButton... naturally you can viewsource your page and see what id it generates and use that but that kinda hardcodes and if you change your control tree layout it might stop working.
im sure there's a way to get around this though...

You can get the '_ctl1$_ctl4$someButton' button's id as follows:

var buttonClientID = '<%=btnServerButton.UniqueID%>';
var htmlButton = document.getElementById (buttonClientID)

Cheers..
 
Reply With Quote
 
maheshsase maheshsase is offline
Junior Member
Join Date: Jun 2007
Posts: 1
 
      06-28-2007
//this code is for Wndows application VS2003 C#
//where txtSearchTerm is a textbox on which you want that Click
//event of btnSearch to get occur.

private void txtSearchTerm_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar.Equals( Convert.ToChar(13) ))
{
this.btnSearch_Click(this.btnSearch,null);
}

}
 
Reply With Quote
 
cathal_mchale cathal_mchale is offline
Junior Member
Join Date: May 2009
Posts: 1
 
      05-18-2009
Quote:
Originally Posted by xanoxate
With asp.net 2.0 you can enlcose the text box and button inside a panel and then set the DefaultButton property of the panel to the id of the button, when the user presses the enter key the button will be pressed

Ok, this is a very old post, but just wanted to point out that the post by xanoxate is the best one for me. Thanks, I never knew this, and always thought that farting about with Javascript and ClientIDs seemed overkill and something that ASP.NET should have provided out-of-the-box, given how common the scenario is!
 
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
When user press F5 after a button click event, the click event fires again! Max2006 ASP .Net 2 09-19-2007 05:37 AM
How to simulate a key press? Joel C Programming 4 05-12-2006 06:58 PM
Help:How to avoid button click event when Enter key is press in a TextBox? Jie ASP .Net 7 10-09-2004 02:12 PM
simulate button click in C# =?Utf-8?B?TWF4?= ASP .Net 5 04-06-2004 08:35 AM
Pressing Enter Key from a Textbox (Netscape 4.79) and simulate a button-click GOKU24 Javascript 0 08-26-2003 07:36 PM



Advertisments