faizan ahmad

Usually the things which were not a straight Google

  Home  |   Contact  |   Syndication    |   Login
  15 Posts | 0 Stories | 55 Comments | 0 Trackbacks

News

Archives

Post Categories

.NET

ASP.NET

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>
 
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Thursday, January 08, 2009 4:15 PM

Feedback

# re: Call OnClick Event of Button when Enter key pressed in TextBox 6/30/2009 1:19 AM Sunil Sutar
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

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

# re: Call OnClick Event of Button when Enter key pressed in TextBox 9/15/2009 2:49 PM Masoni
Thanks great solutions.

# re: Call OnClick Event of Button when Enter key pressed in TextBox 9/29/2009 6:37 AM farhan
Thanks a lot sir..

# re: Call OnClick Event of Button wdhen Enter key pressed in TextBox 2/25/2010 12:17 AM heart
first one panel idea is very very intresting and useful 2

# re: Call OnClick Event of Button when Enter key pressed in TextBox 2/25/2010 12:01 PM Okie
Thank you so much this helped!

# re: Call OnClick Event of Button when Enter key pressed in TextBox 6/8/2010 2:01 AM hiral
Very helpful...Thnx a lot.It also works in version 8 IE & firefox.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: