Geeks With Blogs
faizan ahmad Usually the things which were not a straight Google
Problem: Say you have two Grid View controls on one page, each grid view has a TexBox and a 'search' button above it

TextBox1 btnSearch1                                       TextBox2 btnSearch2

Now if you press 'Enter' key  in TextBox1, it will trigger btnSearch1_click event and when you press 'Enter' key in TextBox2, it still triggers btnSearch1_click


Solution:  Add asp.net Panel tag and set DefaultButton property.

<asp:Panel ID="pnlSearch2" runat="server" DefaultButton="btnSearch2">
          TextBox2 btnSearch2
</asp:Panel>
  Posted on Thursday, January 8, 2009 4:15 PM ASP.NET | Back to top


Comments on this post: Call OnClick Event of Button when Enter key pressed in TextBox

# re: Call OnClick Event of Button when Enter key pressed in TextBox
Requesting Gravatar...
Hi,

Try using following code -

function KeyDownHandler(e, btn) {
var eventInstance = window.event ? event : e;

if (window.event) // IE
{
if (eventInstance.keyCode == 13) {
eventInstance.returnValue = false;
eventInstance.cancel = true;
var obj = document.getElementById(btn);
obj.click();
return false;
}
}
else if (eventInstance.which) // Netscape/Firefox/Opera
{
if (eventInstance.which == 13) {
eventInstance.returnValue = false;
eventInstance.preventDefault();
eventInstance.cancelBubble = true;
var obj = document.getElementById(btn);
obj.click();
return false;
}
}
}


on text keypress, attach the above function and pass the id of the button as second parameter like -

txtTemplateName.Attributes.Add("onkeypress", "KeyDownHandler(event, '" + btnSearch.ClientID + "')");

--
Thanks,
Sunil
Left by Sunil Sutar on Jun 30, 2009 1:19 AM

# re: Call OnClick Event of Button when Enter key pressed in TextBox
Requesting Gravatar...
this code doesn't work on IE 8. FireFox OK...
Left by Serdar Sezgin on Aug 17, 2009 4:32 AM

# re: Call OnClick Event of Button when Enter key pressed in TextBox
Requesting Gravatar...
Thanks great solutions.
Left by Masoni on Sep 15, 2009 2:49 PM

# re: Call OnClick Event of Button when Enter key pressed in TextBox
Requesting Gravatar...
Thanks a lot sir..
Left by farhan on Sep 29, 2009 6:37 AM

# re: Call OnClick Event of Button wdhen Enter key pressed in TextBox
Requesting Gravatar...
first one panel idea is very very intresting and useful 2
Left by heart on Feb 25, 2010 12:17 AM

# re: Call OnClick Event of Button when Enter key pressed in TextBox
Requesting Gravatar...
Thank you so much this helped!
Left by Okie on Feb 25, 2010 12:01 PM

# re: Call OnClick Event of Button when Enter key pressed in TextBox
Requesting Gravatar...
Very helpful...Thnx a lot.It also works in version 8 IE & firefox.
Left by hiral on Jun 08, 2010 2:01 AM

Your comment:
 (will show your gravatar)


Copyright © faizanahmad | Powered by: GeeksWithBlogs.net