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? I开发者_运维技巧f so how would I do it?
Checkout the ASP.Net instructional videos. Should cover most, if not all, of your questions.
AJAX Videos: The Official Microsoft ASP.NET Site
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
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
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.
The simplest way is to warp your code with an UpdatePanel
There are many examples if you google it.
<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
精彩评论