Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > issue with click on button

Reply
Thread Tools

issue with click on button

 
 
graphicsxp
Guest
Posts: n/a
 
      06-06-2006
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;
}

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      06-06-2006
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;
> }
>

 
Reply With Quote
 
 
 
 
graphicsxp
Guest
Posts: n/a
 
      06-06-2006
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;
> > }
> >


 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      06-06-2006
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;
>>> }
>>>

>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fire both event button click and textchanged when button is click and text is changed Amy ASP .Net 0 06-01-2006 02:33 PM
image button click event fires before click event of button Purvi T ASP .Net 0 10-19-2004 06:19 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Building Controls 1 05-28-2004 06:23 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net 2 05-25-2004 11:45 PM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net Datagrid Control 0 05-25-2004 01:45 AM



Advertisments