Test User Test User - 3 months ago 13
Ajax Question

Make ASP.NET AJAX function call synchronous

I have an ASP.NET form that calls a web service using ASP.NET AJAX. How do I make the call synchronous?

// test.aspx

<script type="text/javascript">
function GetDescription(){
var code = document.getElementById("<%=txtCode.ClientID%>").value;
var description =
MyNamespace.MyService.GetDescription(
code, onSuccessGetDescription, null, null);
return false;
}

function OnSuccessGetDescription(result){
var txtDescription =
document.getElementById("<%=txtDescription.ClientID%>");
}
</script>
<script type="text/javascript">
$(document).ready(//function () {

$('#<%=cmdSave.ClientID%>').click(function (e) {
e.preventDefault();
if (Page_ClientValidate()){
// Populate jQuery.UI dialog box with code and description
// and then
$("#confirm-dialog").dialog("open");
}
});

}
</script>

<asp:TextBox ID="txtCode" />
<asp:TextBox ID="txtDescription" />
<asp:Button ID="cmdSave" />


// test.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
txtCode.Attributes["onblur"] = "GetDescription();";
}


The answers to this question that I have seen say that I should set the async attribute to false, but I don't see how to do that here.
ASP.Net Ajax - PageMethods Synchronous call and retrieval of results says that it can't be done. I am trying to get a second opinion.

If I can't make the call synchronous, would it be legal to make GetTransaction poll until the OnSuccess function returns? What would be an efficient (i.e. not CPU-intensive) way to do that?

Answer

JQuery Asynchronous

jQuery.ajax({
url: 'http://example.com/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),
success: function (result) {
   if (result.isOk == false) alert(result.message);
    },
    async: false
});