如何在按钮单击时调用异步?

时间:2019-01-16 07:09:04

标签: c# html asp.net angularjs

我做下面的事情来在clikc上调用asyn

  private List<SuggestedItemsInput> suggestedItemAsync()
    {
        OHDWebService OHDService = new OHDWebService();
        List<SuggestedItemsInput> suggestedItemsList = OHDService.SaveSuggestedItems(ViewState["Body"].ToString(), hfdOrderRecordID.Value);
        return suggestedItemsList;
    }

onclick

  protected async void imgbtnClaim_Click(object sender, EventArgs e)
    {
        try
        {
            Task<List<SuggestedItemsInput>> task = new Task<List<SuggestedItemsInput>>(suggestedItemAsync);
            task.Start();
            lblError.Text = "Please Wait. Proccessing...";
            List<SuggestedItemsInput> suggestedItems = await task;
            if (suggestedItems.Count > 0)
            {

但它阻止了UI,并且出现如下所示的错误提示 enter image description here

如何实现?

1 个答案:

答案 0 :(得分:2)

创建这样的网络方法:

[WebMethod]
public async Task Yourmethod(string value)
{
value = "abc";
  return value;
}

然后从您的前端使用jquery来实现以下方法:

<script>
$(fucntion(){
    $.ajax({
        type: "POST",
        url: "..../Yourmethod",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        success: function (data) { }
    });
});

希望有帮助。