Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

I decided to write this article because I always encounter this kind of problem in the ASPNET Forum frequently. So here's a simple (one way) solution on how to invoke the Button Click event when pressing the ENTER key in the TextBox control.


ASPX Mark Up and JavaScript function

<head>
    <title>Untitled Page</title>
</style>
<script type="text/javascript" language="javascript">

function controlEnter (obj, event)
{      
     var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;      
     if (keyCode == 13)
     {                  
        document.getElementById(obj).click();
                    return false;      
     }      
     else  {
                   return true; 
     } 
}
</script>
</head>
<body>
    <form id="form1" runat="server">

        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />

    </form>
</body>

RELEVANT CODES:

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.Attributes.Add("onkeypress", "return controlEnter('" + Button1.ClientID + "', event)");
}

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("Button was Clicked!");
}

That's it... Hope this post helps you!

 

Technorati Tags: ,,
Posted on Thursday, September 11, 2008 10:31 AM ASP.NET , JavaScript , Tips&Tricks | Back to top


Comments on this post: Forcing Button Click event to fire up when pressing ENTER Key on TextBox

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
The .click() function is not crossbrowser compliant, it will not fire the postback in firefox.

To fix your code you can change the following:
document.getElementById(obj).click(); => __doPostBack(obj, '');

TextBox1.Attributes.Add("onkeypress", "return controlEnter('" + Button1.ClientID + "', event)"); =>
TextBox1.Attributes.Add("onkeypress", "return controlEnter('" + Button1.UniqueID + "', event)");


__doPostBack() is a function that is generated by ASP.NET that takes a control name and arguments, the name you can get from Control.UniqueID, the arguments you can leave blank in most cases. UniqueID will be different than ClientID when the control is inside a usercontrol or a masterpage.
Left by Anon on Sep 11, 2008 11:16 AM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Well hell.....it seems to not be an issue any longer, if you search google http://www.google.com/search?hl=en&q=firefox+click+event&aq=f&oq=

you'll find a lot of people frustrated by the previously unsupported event.
Left by Anon on Sep 12, 2008 11:18 AM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Hi...thanks for this one. I'm still learning javascript so I didn't really understand the code a lot. If you could, explain them to me. But it's alright, if you won't.
Left by Kail on Nov 24, 2008 3:12 PM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Yes,its work very fine
Left by ravi on Jan 25, 2009 9:24 PM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Its working. I am looking for the exact code you provided. Thank you.
Left by Kiran on Feb 09, 2009 10:33 PM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Hi it works fine,thank you for solving my big problem,I was in tension when i had a deadlines and I found the good solution.
Left by Swapnil on Apr 28, 2009 6:18 PM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
I want to achieve this functionality on the whole page, i mean to say i have a asp.net page i have a save button and on pressing Ctrl + S, i want to initiate the save button click event.

Left by guru on May 18, 2009 6:32 AM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
thanks a lot dear.

its really useful.
God bless you.
Left by biraj patel on May 13, 2010 7:33 PM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Thanks man!!! It works fine in IE7, FireFox 3.6, Safari 3.1. It saved my lot of time. It's really useful. Keep doing such good things and god will be with u.
Left by Rajesh on Nov 23, 2010 3:11 PM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Thanks. It works also in a webpart.

Left by Mike on Feb 16, 2011 5:20 PM

# re: Forcing Button Click event to fire up when pressing ENTER Key on TextBox
Requesting Gravatar...
Thanks bro' useful..
Left by rakesh on Dec 06, 2011 5:03 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net