issue with click on button

Discussion in 'ASP .Net' started by graphicsxp, Jun 6, 2006.

  1. graphicsxp

    graphicsxp Guest

    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_btnPublication").click();
    }
    return false;
    }
    else
    return true;
    }
    graphicsxp, Jun 6, 2006
    #1
    1. Advertising

  2. 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_btnPublication").click();
    > }
    > return false;
    > }
    > else
    > return true;
    > }
    >
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 6, 2006
    #2
    1. Advertising

  3. graphicsxp

    graphicsxp Guest

    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_btnPublication").click();
    > > }
    > > return false;
    > > }
    > > else
    > > return true;
    > > }
    > >
    graphicsxp, Jun 6, 2006
    #3
  4. 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_btnPublication").click();
    >>> }
    >>> return false;
    >>> }
    >>> else
    >>> return true;
    >>> }
    >>>

    >
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    820
    Purvi T
    Oct 19, 2004
  2. mike parr
    Replies:
    1
    Views:
    458
    Marina
    May 20, 2004
  3. Ben Fidge
    Replies:
    5
    Views:
    719
    S. Justin Gengo
    Jun 15, 2004
  4. Amy
    Replies:
    0
    Views:
    954
  5. Replies:
    0
    Views:
    285
Loading...

Share This Page