[英]How to use jQuery ajax requests to trigger a code behind event handler
我有这个ajax面板:
<telerik:RadAjaxPanel ID="rap" runat="server">
...
<-- Contains several Hidden fields -->
</telerik:RadAjaxPanel>
在后面的代码中,我有一个事件处理程序,使用以下签名处理面板上的所有ajax请求:
Private Sub rap_AjaxRequest(sender As Object, e As Telerik.Web.UI.AjaxRequestEventArgs) Handles rap.AjaxRequest
我可以像这样从Javascript成功调用ajax请求:
var panel = $find('<%=rap.ClientID%>');
panel.ajaxRequest();
调用ajax请求的方法需要隐藏代码之一的值,该值在后面的代码中设置。 这样做不能正确地将值传递到客户端,因为它是异步的。 我正在尝试使用jQuery来像这样同步触发请求:
var panel = $find('<%=rap.ClientID%>');
$.ajax({
async: false,
context: panel
}).done(function() {
// Handle the rest of the function if the ajax call succeeded
});
当我单步执行代码时,发现我的.done函数运行时,ajax调用成功。 但是,后面代码中处理请求的方法永远不会触发。 如何获得此调用以正确触发正在由我的代码处理的事件?
我创建了一个工作示例,展示了我“想”您正在尝试做的事情。 尝试这个。
JavaScript的:
<script>
function submitViaAjax() {
var rap = $find('<%=rap.ClientID%>');
rap.ajaxRequest();
}
function rap_onResponseEnd(sender, arg) {
alert(document.getElementById('<%=hdnValue.ClientID%>').value);
}
</script>
ASPX
<telerik:RadAjaxPanel ID="rap" runat="server"
ClientEvents-OnResponseEnd="rap_onResponseEnd">
<asp:HiddenField ID="hdnValue" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return submitViaAjax();" />
</telerik:RadAjaxPanel>
后面的代码:
Protected Sub rap_AjaxRequest(sender As Object, e As Telerik.Web.UI.AjaxRequestEventArgs) Handles rap.AjaxRequest
hdnValue.Value = "123456"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.