简体   繁体   English

如何使用Ajax调用Codebehind函数而无需回发

[英]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. 我希望能够在不重新加载页面的情况下调用MyFunction。 Is this possible with ajax or something? 使用ajax或其他东西可能吗? If so how would I do it? 如果可以,我该怎么办?

Checkout the ASP.Net instructional videos. 观看ASP.Net指导视频。 Should cover most, if not all, of your questions. 应该涵盖大部分(如果不是全部)问题。

AJAX Videos: The Official Microsoft ASP.NET Site AJAX视频:官方Microsoft ASP.NET网站

One option is to use page methods . 一种选择是使用页面方法

Add a static method to your page, decorated with the WebMethod attribute: 在您的页面上添加一个用WebMethod属性装饰的静态方法:

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

Enable page methods in your ScriptManager : 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): 然后,您可以使用JavaScript从客户端调用它(可以将其连接到按钮的OnClientClick事件):

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 有关更多详细信息,请阅读此页上的“在ASP.NET网页中调用静态方法”部分: http : //msdn.microsoft.com/zh-cn/library/bb398998.aspx

You have to download Ajax Extensions and install it. 您必须下载并安装Ajax Extensions

After you do this place instead <asp:Button OnClick="MyFunction" runat="server" /> the following 在执行此操作后,改为<asp:Button OnClick="MyFunction" runat="server" />以下内容

    <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 ac#/vb function written in your page behind code, not a javascript function. MyFunction是在页面后面的代码中编写的ac#/ vb函数,而不是javascript函数。 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. OnClick属性用于c#/ vb函数,OnClientClick用于javascript函数。

For more information regarding how to add ajax functionality to an ASP .NET Page go here 有关如何将ajax功能添加到ASP .NET页的更多信息,请转到此处。

The simplest way is to warp your code with an UpdatePanel 最简单的方法是使用UpdatePanel修改代码

There are many examples if you google it . 如果您用google搜索,有很多示例。

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. 是的,您可以使用此功能与Asp.net Ajax一起使用“进行客户端网络回调”,而无需重新加载页面即可在方法后面调用代码。 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 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. 使用jQuery Ajax还有一个选择。

<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM