开发者

Asp.Net - JQuery ajax call button action

Let say I got the following button : <asp:Button ID="btnSearch" runat="server" /> and the following button event han开发者_如何学Cdler :

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

How, with JQuery, can I call the button event handler ?

What I mean is that I don't want the page to refresh, so it's kind of Ajax call. I don't want to simulate the click but that on click the button event handler is call.


$("#<%= btnSearch.ClientID %>").click();

UPDATE

There are many ways of doing this asynchronously. You could have your button be set up as a trigger for an UpdatePanel, and then my original answer would still work. I wouldn't do that, but that's because I hate UpdatePanels.

You could create a page method in your code behind class, like this:

[WebMethod]
public static void Search()
{
    // Do search
}

and in your ScriptManager (you'll have to add one if you don't have it), enable page methods.

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

Then, you don't even need a server control for your button. Just use a plain old button.

<input type="button" onclick="search()" value="Search" />

// Then in javascript...
function search()
{
    PageMethods.Search(function(result)
    {
        // deal with search result here (this is the success handler)
    });
}

Or you could call your page method directly from jquery, as shown by this Encosia article.

Or, you could have a completely separate service, not part of your code behind, that encapsulates your search logic, and you could call it any number of ways.

Since you've updated your question, you're question isn't really about how to execute your button's click handler, it's about how to do an async operation. And it's a little vague. There are a million ways to do that.


I would advise staying very far away from Microsoft's AJAX as it is really heavy. jQuery has a really easy implementation and if you just need a simple async callback pagemethods are the way to go.

Also to save your self some hassle check out the "json2" library for json serialization of everything. I haven't had it break on me yet and I have had to serialize some fairly complex objects. link text

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜