Click checkbox's label - javascript onclick gets called twice

Discussion in 'ASP .Net Web Controls' started by, May 22, 2005.

  1. Guest

    This is intriguing - I've copied over my old fully working ASP.NET
    project from a Win 2000 Pro box to an XP Pro box. Everything seems to
    work fine, except I've noticed one difference. I have a server side
    checkbox with a javascript onclick event attribute attached to it.
    When you click the checkbox's *label*, on the new XP box, the
    javascript onclick event gets called twice, with undesired
    consequences. This odd side effect does not occur if you click the
    checkbox's box itself. On the Win 2000 box, this side effect does not
    occur at all.

    Any thoughts please? Thanks, regards, dnw.
    , May 22, 2005
    1. Advertisements

  2. sam

    sam Guest

    I think you may be posting *2* requests to the server by double
    clicking or something. I had this problem once by using an input
    type=image with a dopostback click event handler.

    Check the web server logs for double get requests very close together
    in time. You can see where these are by going into IIS and looking for
    the log file paths property or whatever.

    This saved me a lot of time.

    -Sam Robertson
    sam, May 22, 2005
    1. Advertisements

  3. Guest

    In the iis log file, there's only one GET for my test (click)

    Thanks for the idea though.

    Any other thoughts please? Thanks, regards, dnw.
    , May 22, 2005
  4. Guest

    PS: I've just set up a completely new test project with 2 lines of code
    to do this, and the javascript function still gets called twice.

    I think this is only happening on my new development machine locally,
    because before I changed operating systems, I uploaded my old solution
    to the internet, and it works correctly using the new XP operating
    , May 22, 2005
  5. sam

    sam Guest

    Now my guess is that the onclick in the label html is being executed
    twice, maybe because of a reinterpretation of event bubbling in
    javascript. Can you post the complete html for the checkbox and label?
    Is it the standard control?
    sam, May 23, 2005
  6. Guest

    Sam, I am using a standard server side checkbox.

    Server side code (there's only 1 line of code)

    CheckBox1.Attributes.Add("onclick", "myfunc()")

    HTML code (when in debug mode)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
    <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content=""
    <script language="javascript">
    function myfunc()

    //This function gets called twice when you click the label portion
    of the checkbox.
    <body MS_POSITIONING="GridLayout">
    <form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
    <input type="hidden" name="__VIEWSTATE"

    <span onclick="myfunc()" style="Z-INDEX: 101; LEFT: 271px; POSITION:
    absolute; TOP: 205px"><input id="CheckBox1" type="checkbox"
    name="CheckBox1" /><label for="CheckBox1">Click me here</label></span>

    The really interesting thing is that if I upload this test project to a
    server on the internet, it works fine - the javascript function only
    gets called once.

    Thank you, regards -dnw.
    , May 24, 2005
    1. Advertisements

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. Matt Hawley

    button's click handler gets called twice

    Matt Hawley, Apr 7, 2004, in forum: ASP .Net
    Nikhil Patel
    Apr 7, 2004
  2. mike parr
    May 20, 2004
  3. Replies:
  4. Replies:
    prasanth k
    Oct 5, 2009
  5. Knut Krueger
    Knut Krueger
    Aug 20, 2013

Share This Page