473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Wrong button.click function is trigger when press Enter key on textbox

18
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the page has multiple button such login page with a search function somewhere around, then it's not respond properly. I have attached a brief example of two text boxes and two button. When ever a key is press on textbox one, I want to trigger button1.click, and press on text box 2 to trigger button2.click. The id and button is correct from the alert but i don't know why it keep calling button1.click event when I press on textbox 2.

<script type="text/javascript">

//Handle enter key press to trigger button click
function focusNext(objEvent, strElement)
{
if ( objEvent.keyCode == 13)
{
oNextObj = document.getElementById(strElement);
alert("Enter key is pressed!!");
if( oNextObj ) {
oNextObj.focus();
// alert("Focused on Object and before click() trigger");
oNextObj.click();
alert("processed clicked on ID: " + strElement + " Value: " + oNextObj.value);

} //end if

return;

} //end if

} //end function

</script>
.
.
.
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button 1" /><br />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button 2" /></div>
</form>
</body>

-- vb code


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "Text will display here<br>Click on the text box then press enter key to see change"
Me.TextBox1.Text = "Textbox 1"
Me.TextBox2.Text = "Textbox 2"

Me.TextBox1.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button1.ClientID & "')")
Me.TextBox2.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button2.ClientID & "')")
End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Label1.Text = "Enter key is pressed from Textbox 2"
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Label1.Text = "Enter key is pressed from Textbox 1"
End Sub

End Class
Apr 7 '08 #1
1 6938
shweta123
692 Expert 512MB
Hi,

May be you are sending the wrong Id for button while calling javascript function. You can try using ID instead of ClientID.
e.g

Me.TextBox2.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button2.ID & "')")



I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the page has multiple button such login page with a search function somewhere around, then it's not respond properly. I have attached a brief example of two text boxes and two button. When ever a key is press on textbox one, I want to trigger button1.click, and press on text box 2 to trigger button2.click. The id and button is correct from the alert but i don't know why it keep calling button1.click event when I press on textbox 2.

<script type="text/javascript">

//Handle enter key press to trigger button click
function focusNext(objEvent, strElement)
{
if ( objEvent.keyCode == 13)
{
oNextObj = document.getElementById(strElement);
alert("Enter key is pressed!!");
if( oNextObj ) {
oNextObj.focus();
// alert("Focused on Object and before click() trigger");
oNextObj.click();
alert("processed clicked on ID: " + strElement + " Value: " + oNextObj.value);

} //end if

return;

} //end if

} //end function

</script>
.
.
.
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button 1" /><br />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button 2" /></div>
</form>
</body>

-- vb code


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "Text will display here<br>Click on the text box then press enter key to see change"
Me.TextBox1.Text = "Textbox 1"
Me.TextBox2.Text = "Textbox 2"

Me.TextBox1.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button1.ClientID & "')")
Me.TextBox2.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button2.ClientID & "')")
End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Label1.Text = "Enter key is pressed from Textbox 2"
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Label1.Text = "Enter key is pressed from Textbox 1"
End Sub

End Class
Apr 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Miquel | last post by:
Hi all. I felt frustrated when developing an 'UserControl' derivated from textBox, because sequence event (and Validate event) seems to fail. I Always thought my code was wrong. But after...
1
by: Raghuvansh | last post by:
I have the following simple code of two panels and two pairs of textboxes and buttons. <form id="Form1" method="post" runat="server"> <asp:Panel id="Panel1" runat="server" Width="272px"...
5
by: Steve | last post by:
Hi, Is it possible to make hitting the enter key in an ASP textbox run the code behind an ASP button on a form? I have a search page which users tend to type in the query then just hit enter...
5
by: Girish | last post by:
I have TWO submit buttons of type IMAGE on my asp form. This renders as <input type="image">. I need to be able to eble the ENTER button for both buttons. Yes, I know that for the input type...
2
by: Lenard Gunda | last post by:
Hi! I have an ASP.NET page with multiple TextBox fields and buttons. When I press the <enter> key in a TextBox, the page postbacks, and one of the buttons get fired (OnClick). Now certain...
5
by: TS | last post by:
for some reason, it posts to the server, but no click events of any buttons on form fire. the button is the first one on the form. when the focus is inside the textbox, it doesnt' work. if i click...
2
by: Lenster | last post by:
Environment --------------- Visual Studio.NET 2003 Version 7.1.3088 ..NET Framework 1.1 Version 1.1.4322 SP1 XP Professional 5.1.2600 SP2 Build 2600 Problem Description...
3
by: graphicsxp | last post by:
Hi, I've written some javascript function so that if a textbox has the focus and the user press enter, it triggers a click on a button 'associated' to this textbox (see code at the end) The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.