2017-04-08 67 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加載數據。 –