473,385 Members | 1,582 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,385 software developers and data experts.

issue with click on button

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 javascript works fine. If I'm in txtJournalist2 and I press enter,
the OnClick Server event of btnAuthor2 is triggered. But the problem is
that for some reason, the OnClick Server event of btnAuthor is also
triggered afterward (not from the javascript as no alert popup is
shown).

In fact if I invert the place of the buttons in the page, so that
btnAuthor2 is BEFORE btnAuthor then there is always a call to the
OnClick event of btnAuthor2 even if I was in the first textbox when I
pressed Enter......

Does anyone know why ?

Thanks

<asp:TextBox ID="txtJournalist" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor" runat="server" Text="Search..."
CausesValidation="False" />

<asp:TextBox ID="txtJournalist2" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor2" runat="server" Text="Search..."
CausesValidation="False" />&nbsp;


function fct(evt, obj)
{
evt = (evt)? evt : event
var charCode = (evt.which) ? evt.which : evt.keyCode
if(charCode == 13)
{
if (obj.name == 'ctl00$cpBody$txtJournalist')
{
alert('ctl00$cpBody$txtJournalist');
document.getElementById("ctl00_cpBody_btnAuthor"). click();
}
else if (obj.name == 'ctl00$cpBody$txtJournalist2')
{
alert('ctl00$cpBody$txtJournalist2');
document.getElementById("ctl00_cpBody_btnAuthor2") .click();
}
else if (obj.name == 'ctl00$cpBody$txtPublication')
{
alert('ctl00$cpBody$txtPublication');

document.getElementById("ctl00_cpBody_btnPublicati on").click();
}
return false;
}
else
return true;
}

Jun 6 '06 #1
3 1540
If you press enter, the first button in the form is used to post the
form. As the form is already being posted, the event sent by the click()
method is never handled.

graphicsxp wrote:
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 javascript works fine. If I'm in txtJournalist2 and I press enter,
the OnClick Server event of btnAuthor2 is triggered. But the problem is
that for some reason, the OnClick Server event of btnAuthor is also
triggered afterward (not from the javascript as no alert popup is
shown).

In fact if I invert the place of the buttons in the page, so that
btnAuthor2 is BEFORE btnAuthor then there is always a call to the
OnClick event of btnAuthor2 even if I was in the first textbox when I
pressed Enter......

Does anyone know why ?

Thanks

<asp:TextBox ID="txtJournalist" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor" runat="server" Text="Search..."
CausesValidation="False" />

<asp:TextBox ID="txtJournalist2" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor2" runat="server" Text="Search..."
CausesValidation="False" />&nbsp;


function fct(evt, obj)
{
evt = (evt)? evt : event
var charCode = (evt.which) ? evt.which : evt.keyCode
if(charCode == 13)
{
if (obj.name == 'ctl00$cpBody$txtJournalist')
{
alert('ctl00$cpBody$txtJournalist');
document.getElementById("ctl00_cpBody_btnAuthor"). click();
}
else if (obj.name == 'ctl00$cpBody$txtJournalist2')
{
alert('ctl00$cpBody$txtJournalist2');
document.getElementById("ctl00_cpBody_btnAuthor2") .click();
}
else if (obj.name == 'ctl00$cpBody$txtPublication')
{
alert('ctl00$cpBody$txtPublication');

document.getElementById("ctl00_cpBody_btnPublicati on").click();
}
return false;
}
else
return true;
}

Jun 6 '06 #2
Fair enough, but then what is the workaround ?
Göran Andersson wrote:
If you press enter, the first button in the form is used to post the
form. As the form is already being posted, the event sent by the click()
method is never handled.

graphicsxp wrote:
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 javascript works fine. If I'm in txtJournalist2 and I press enter,
the OnClick Server event of btnAuthor2 is triggered. But the problem is
that for some reason, the OnClick Server event of btnAuthor is also
triggered afterward (not from the javascript as no alert popup is
shown).

In fact if I invert the place of the buttons in the page, so that
btnAuthor2 is BEFORE btnAuthor then there is always a call to the
OnClick event of btnAuthor2 even if I was in the first textbox when I
pressed Enter......

Does anyone know why ?

Thanks

<asp:TextBox ID="txtJournalist" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor" runat="server" Text="Search..."
CausesValidation="False" />

<asp:TextBox ID="txtJournalist2" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor2" runat="server" Text="Search..."
CausesValidation="False" />&nbsp;


function fct(evt, obj)
{
evt = (evt)? evt : event
var charCode = (evt.which) ? evt.which : evt.keyCode
if(charCode == 13)
{
if (obj.name == 'ctl00$cpBody$txtJournalist')
{
alert('ctl00$cpBody$txtJournalist');
document.getElementById("ctl00_cpBody_btnAuthor"). click();
}
else if (obj.name == 'ctl00$cpBody$txtJournalist2')
{
alert('ctl00$cpBody$txtJournalist2');
document.getElementById("ctl00_cpBody_btnAuthor2") .click();
}
else if (obj.name == 'ctl00$cpBody$txtPublication')
{
alert('ctl00$cpBody$txtPublication');

document.getElementById("ctl00_cpBody_btnPublicati on").click();
}
return false;
}
else
return true;
}


Jun 6 '06 #3
Make sure that the keypress event is stopped from posting the form. IIRC
you use cancelBubble to do that.

graphicsxp wrote:
Fair enough, but then what is the workaround ?
Göran Andersson wrote:
If you press enter, the first button in the form is used to post the
form. As the form is already being posted, the event sent by the click()
method is never handled.

graphicsxp wrote:
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 javascript works fine. If I'm in txtJournalist2 and I press enter,
the OnClick Server event of btnAuthor2 is triggered. But the problem is
that for some reason, the OnClick Server event of btnAuthor is also
triggered afterward (not from the javascript as no alert popup is
shown).

In fact if I invert the place of the buttons in the page, so that
btnAuthor2 is BEFORE btnAuthor then there is always a call to the
OnClick event of btnAuthor2 even if I was in the first textbox when I
pressed Enter......

Does anyone know why ?

Thanks

<asp:TextBox ID="txtJournalist" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor" runat="server" Text="Search..."
CausesValidation="False" />

<asp:TextBox ID="txtJournalist2" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor2" runat="server" Text="Search..."
CausesValidation="False" />&nbsp;


function fct(evt, obj)
{
evt = (evt)? evt : event
var charCode = (evt.which) ? evt.which : evt.keyCode
if(charCode == 13)
{
if (obj.name == 'ctl00$cpBody$txtJournalist')
{
alert('ctl00$cpBody$txtJournalist');
document.getElementById("ctl00_cpBody_btnAuthor"). click();
}
else if (obj.name == 'ctl00$cpBody$txtJournalist2')
{
alert('ctl00$cpBody$txtJournalist2');
document.getElementById("ctl00_cpBody_btnAuthor2") .click();
}
else if (obj.name == 'ctl00$cpBody$txtPublication')
{
alert('ctl00$cpBody$txtPublication');

document.getElementById("ctl00_cpBody_btnPublicati on").click();
}
return false;
}
else
return true;
}

Jun 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
6
by: Mike | last post by:
I have a function that is called when the user clicks the submit button, during this function i also set a varaible to "Y" due to that this function does a post back to the page then redirects....
9
by: PK9 | last post by:
I'm having an issue with the "Refresh" of an asp.net page. The refresh is actually calling my last onClick event. I thought that asp.net was supposed to be stateless in that it shouldn't...
10
by: Li Pang | last post by:
Hi, I created a html page from which I give a link to another web site. The new site is opened in a new window. When I opened multiple windows, they all have the same SessionID. I want ot know...
12
by: raghav | last post by:
Hi I am working on ASP.NET 2.0. I am developing a website using Wizard control. Based on number of steps added, next, previous, finish buttons generate automatically. After running the...
11
by: =?Utf-8?B?R29rdWw=?= | last post by:
I am struck up with a problem and want anyone here to help me out. I am a beginner in .NET trying to learng DataBinding concepts. I have binded 4 text boxes with a dataset but when I say...
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
2
by: Paul | last post by:
I've written som junk code below to show a problem I'm getting: On a page I've got a dropdownlist and a button using submit behaviour: <asp:DropDownList ID="tester" runat="server"...
1
by: chaitanyadotcom | last post by:
As per my application i need to create tabs using iFrame dynamically. There are totally 4 buttons in my application where for each button i provide a link. Where in it will dynamically create a tab...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.