开发者

click() not behaving like user click

I have searched for a solution to this for the last several hours but to no avail. When I click on a button that has a return false in OnClientClick, no postback occurs to the server. When I use jquery to trigger the click function of the button, OnClientClick fires first, but regardless of the return value, a postback occurs. Here's a simple sample...

k...Putting code that more accurately demonstrates the problem. Sorry for the mixup...

    &l开发者_Go百科t;html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="script/jquery-1.4.2.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <img onclick="$('#Button1').click();" runat="server" width="100" height="100" style="border:solid 1px black" /><br />
        <asp:Button ID="Button1" OnClick="Button1_Click" OnClientClick="return false;" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

To answer some questions that were asked...I have a User Control. It is an rollover button using two images and a hidden button to call the server. The button uses Reflection to enable whatever OnClick handler is specified to call the function on the particular page that the control is on. The OnClientClick is also used to provide additional Javascript actions if I need them. Everything was working fine when I had an invisible but clickable button directly wired up. I decided to make the button truly invisible and not even display it, but that required me to have the Images actually fire the click event to the button.


The runat="server" indicates that a postback is required. If you're not getting it for Button1, it's probably because you have an OnClick and an OnClientClick in the same tag and the combination is overriding the runat attribute.


jQuery's click method doesn't return true or false like the OnClick method does - it instead returns a reference to itself. This is to facilitate chaining of jQuery method calls.

Ideally what you want is to call the same method from both Button1 and Button2's javascript instead of just having one button call the other. If you need some condition where a postback IS required, you can stick a call to Button1.GetPostbackEventReference in there.

jQuery also makes this really easy if you use a CSS class to handle it:

<asp:Button ID="Button1" CssClass="ButtonHandler" />
<asp:Image ID="Button2" CssClass="ButtonHandler" />

<script>
$(document).ready(function() {
    $(".ButtonHandler").click( function() {
        // stuff you want to do
        return false;
    });
});
</script>

This may not be exactly what you need, but I think you need to go into a bit more detail into what exactly you want.


I think you've misunderstood the purpose of the jQuery click handler in this context, and you're not returning false in the onClientClicked event.

There is no reason to use the onClientClicked property to wire up a jQuery click handler for a server side button or image. Just wire up a script call in there to whatever script you need to execute.

Like this:

<asp:Button ID="Button2" runat="server" Text="Trigger" onClientClick="doClientSideCheck();" />

<script type="text/javascript">
  function doClientSideCheck()
  {
    return false;
  }
</script>

On the other hand when you have client side button or image this is where the jQuery click handler is appropriate.

<input id="button1" type="button" class="myButtonClass" value="Submit" />

    <script type="text/javascript">
     $(document).ready(function()
        {
             $(".myButtonClass").click(function(e) { alert('button clicked ' + $(this).attr('id') ); });
         });
    </script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜