tags:

views:

1287

answers:

6

Why doesn't this work?

    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('.myButton').click();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton id="ttt" runat="server" PostBackUrl="~/Default.aspx" CssClass="myButton">Click</asp:LinkButton>
    </div>
    </form>
A: 

you need to assign an event handler to fire for when the click event is raised

    $(document).ready(function() {
        $('.myButton', '#form1')
            .click(function() {  
                /* 
                   Your code to run when Click event is raised.
                   In this case, something like window.location = "http://..."                      
                   This can be an anonymous or named function
                */
                return false; // This is required as you have set a PostbackUrl
                              // on the LinkButton which will post the form
                              // to the specified URL
            }); 
    });

I have tested the above with ASP.NET 3.5 and it works as expected.

There is also the OnClientClick attribute on the Linkbutton, which specifies client side script to run when the click event is raised.

Can I ask what you are trying to achieve?

Russ Cam
Thanks but did you try that..that doesn't work
Hitz
it does work, you need to perform some action in the function. are you adding the linkbutton through AJAX or after the Page has loaded?
Russ Cam
is the jQuery framework referenced correctly? You can test by trying $(document).ready(function() { alert("Hello"); });
Russ Cam
A: 

The click event handler has to actually perform an action. Try this:

$(function () {
    $('.myButton').click(function () { alert('Hello!'); });
});
Andrew Noyes
Thanks but did you try that..that doesn't work
Hitz
No, I don't have an ASP environment to test it on. It is definitely an error, though.
Andrew Noyes
A: 

you need to give the linkButton a CssClass="myButton" then use this in the top

$(document).ready(function() {
        $('.myButton').click(function(){
             alert("hello thar");
        });
    });
Ryan Rohrer
+3  A: 

Do you want to submit the form, or add a Click event? Your link button translates to

<a id="ttt" class="myButton" href="javascript:WebForm_DoPos[...]">Click</a>

, so it has no on-click javascript. Therefore, .click(); does nothing.
I haven't test it, but maybe this will work:

eval($('.myButton').attr('href'));
Kobi
.click() wouldn't do anything anyway since jQuery uses event listeners rather than adjusting the .onclick element property.
Andrew Noyes
Actually, Andrew, I tested it just to be sure, and .click() does start the onClick event.
Kobi
Oh, sorry, my bad!
Andrew Noyes
A: 

That's a tough one. As I understand it, you want to mimic the behavior of clicking the button in javascript code. The problem is that ASP.NET adds some fancy javascript code to the onclick handler.

When manually firing an event in jQuery, only the event code added by jQuery will be executed, not the javascript in the onclick attribute or the href attribute. So the idea is to create a new event handler that will execute the original javascript defined in attributes.

What I'm going to propose hasn't been tested, but I'll give it a shot:

$(document).ready(function() {

// redefine the event
$(".myButton").click(function() {
   var href = $(this).attr("href");

   if (href.substr(0,10) == "javascript:") {
      new Function(href.substr(10)).call(this);
      // this will make sure that "this" is
      // correctly set when evaluating the javascript
      // code
   } else {
      window.location = href;
   }

   return false;
});

// this will fire the click:

$(".myButton").click();

});
Philippe Leybaert
A: 

postbackurl and onclick event doesnt work well in linkbutton

ramz