anthonypliu
anthonypliu

Reputation: 12437

How to call codebehind function without postback with ajax

I have a controller:

<asp:Button OnClick="MyFunction" runat="server" />

I want to be able to call MyFunction without the page reloading. Is this possible with ajax or something? If so how would I do it?

Upvotes: 0

Views: 19935

Answers (6)

Xavier Poinas
Xavier Poinas

Reputation: 19743

One option is to use page methods.

Add a static method to your page, decorated with the WebMethod attribute:

[WebMethod]
public static void MyMethod(string someParam) 
{
}

Enable page methods in your ScriptManager:

<asp:ScriptManager EnablePageMethods="True" ... />

And then you can call it from the client side, using JavaScript (that you can wire to the OnClientClick event of your button):

PageMethods.MyMethod("some value", successCallback, errorCallback);

For more details read the "Calling Static Methods in an ASP.NET Web Page" section on this page: http://msdn.microsoft.com/en-us/library/bb398998.aspx

Upvotes: 1

Michael
Michael

Reputation: 659

You have to download Ajax Extensions and install it.

After you do this place instead <asp:Button OnClick="MyFunction" runat="server" /> the following

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Text="Your Text" OnClick="MyFunction" />
        </ContentTemplate>
    </asp:UpdatePanel>

MyFunction would be a c#/vb function written in your page behind code, not a javascript function. I specified this because i've seen many mistakes that people make regarding this one. OnClick attribute is for c#/vb function and OnClientClick is for javascript function.

For more information regarding how to add ajax functionality to an ASP .NET Page go here

Upvotes: 1

r12
r12

Reputation: 59

<asp:Button runat="server" ID="btnShowModal" Text="Show" 
     OnClick="btnShowModal_Click" /> 
<asp:Button runat="server" ID="HiddenForModal" style="display: none" />
<ajaxToolKit:ModalPopupExtender ID="Modal1" runat="server" 
     TargetControlID="HiddenForModal" PopupControlID="PopupPanel" />......it may help u

Upvotes: 0

Nitish Katare
Nitish Katare

Reputation: 1165

Yes you can use Make Client-Side Network Callbacks with Asp.net Ajax using this function you can call your code behind methods without reloading your page. To use this methods you should write your methods as static type.

You can refer this video

http://www.asp.net/ajax/videos/how-do-i-make-client-side-network-callbacks-with-aspnet-ajax

You have one more option using the jQuery Ajax.

Upvotes: 0

Aristos
Aristos

Reputation: 66641

The simplest way is to warp your code with an UpdatePanel

There are many examples if you google it.

Upvotes: 0

Longstream
Longstream

Reputation: 21

Checkout the ASP.Net instructional videos. Should cover most, if not all, of your questions.

AJAX Videos: The Official Microsoft ASP.NET Site

Upvotes: 1

Related Questions