2017-04-08 70 views
2

我有一个简单的页面和事件列表。点击按钮后,需要转到“与会者”页面并在该事件中加载人员。page_load上的相当简单的asp.net异步操作阻塞UI

现在发生的情况是,单击按钮后,事件页面将保持可见状态,直到所有与会者处理完成并完成,然后显示结果。我希望它首先显示来自第一个异步调用的事件名称,然后我可以在页面上显示进度条,以显示正在构建的与会者列表。如果直接使用查询字符串中的事件ID加载“与会者”页面,它将根据需要运行,并显示事件名称和进度条。

我不知道如何设置(异步noob)来获得所需的结果,并会欣赏一些指针请!

在events.aspx.cs按钮单击处理程序:

Response.Redirect("Attendees.aspx?eventID=" + eventID, false); 

然后在Attendees.aspx.cs的Page_Load:

protected void Page_Load(object sender, EventArgs e) 
    { 
     processEvent(); 
     //I've tried this with .Wait() at the end too 
    } 

    private async Task<bool> processEvent() 
    { 
     try 
     { 
      EFEventDetails efEvent = await getEventByIdAsync(); 
      if (efEvent.responseCode != 200) //only 200 is a valid response code for a found event 
      { 
       pnl_loadingError.Visible = true; 
       pnl_attendees.Visible = false; 
      } 
      else 
      { 
       //valid event - carry on. Display event name on page. 
       lbl_EventHeader.Text = efEvent.data.eventID.ToString() + " - " + efEvent.data.eventName; 

       //Now get list of attendees 
       List<vmAttendees> res = await getAttendeesAsync(); 
       if (res.Count > 0) 
       { 
        pnl_AttendeeList.Visible = true; 
        rpt_AttendeeList.DataSource = res; 
        rpt_AttendeeList.DataBind(); 
       } 

      } 

     } 
     catch (Exception ex) 
     { 

      lbl_Result.Text = ex.Message; 
      lbl_Result.ForeColor = System.Drawing.Color.Red; 
      pnl_processResult.Visible = true; 
     } 

     return true; 
    } 

    protected async Task<EFEventDetails> getEventByIdAsync() 
    { 
     var eventsForceService = new EventsForceService(_eventsForceRepo); 
     return await eventsForceService.getEventByIdAsync(_eventID); 
    } 

    protected async Task<List<vmAttendees>> getAttendeesAsync() 
    { 
     var eventsForceService = new EventsForceService(_eventsForceRepo); 
     return await eventsForceService.GetAttendeesByEventIDAsync(_eventID); 
    } 
+0

这不会解决你的如何获得工作的进度条问题,但你应该阅读[文章](HTTPS ://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx)在'Page_Load'中看到正确的事情(你需要调用RegisterAsyncTask(processEvent());确保你的aspx页面有'Async =如果你只是想等到页面为fu,那么它就是“Page”元素中的“true”在展示任何东西之前装载。 –

+0

谢谢斯科特。如果你检查我的原始帖子,你可以看到我已经有进度栏的东西工作得很好,如果我只是直接加载页面。我正在使用SignalR和事件处理程序。我正在阅读您现在发布的文章。谢谢。 – TheMook

回答

2

就拿与会者加载了Page_Load使用的APIController和负载在页面加载后使用jQuery Ajax的数据。

的API控制器例如:

public class AttendeesController : ApiController 
{ 
    Attendee[] attendees = new Attendee[] 
    { 
     new Attendee { Id = 1, Name = "Mike" }, 
     new Attendee { Id = 2, Name = "Steve" }, 
     new Attendee{ Id = 3, Name = "Diane" } 
    }; 

    public IEnumerable<Attendee> GetAllAttendees() 
    { 
     // This is using a premade list of Attendees 
     // but you would get them here and return them 

     // Will convert to JSON automatically 
     return attendees; 
    } 
} 

的AJAX负载

<script type="text/javascript"> 
    $.get("Attendees/GetAllAttendees", function(data) { 
     // Load the data into the page after it returns 
     $(".result").html(data); 
     alert("Load was performed."); 
    }); 
</script> 
+0

如果需要,我很高兴做一个ajax调用,但我试图在这种情况下尽可能避免它。这真的是实现我的目标的唯一方式吗?是否没有办法单独使用C#代码? – TheMook

+0

如果你想要一个更新进度条,因为它被加载,它将需要某种类型的AJAX。 –

+3

页面加载完成服务器端。该页面在完成之前不会加载。所以如果有数据通话,它会一直等待它。要在不等待通话的情况下加载页面,您需要将页面返回给客户端,然后使用AJAX加载数据。 –