2012-06-08 55 views
0

我使用ASP.NET從代碼隱藏更改過程值?

我想在數據庫工作時從代碼隱藏中向用戶顯示百分比。

我想問題是在這裏,當我從cs percentege調用這個函數工作精美!

protected void btnUpdate_Click(object sender, EventArgs e) 
    { 
     System.Threading.Thread.Sleep(5000); 
    } 

但我真正的代碼是連接數據庫並插入300 - 1000行! 它工作時服務器光標圖標變爲忙圖標,所以它凍結,我不能設置我的百分比值。

plz幫助...

我有一個Web服務:

public double CommittedCount = 0; 

    [WebMethod] 
    public string ShowPercentage() 
    { 
     return ((CommittedCount/FinishCount) * 100).ToString(); 
    } 

    [WebMethod] 
    public void SetCommittedCount(double committedCount) 
    { 
     CommittedCount = committedCount; 
    } 


    public double FinishCount 
    { 
     get 
     { 
       if(Glob.ExcelDataSet.Tables[0].Rows.Count > 0) 
        return Glob.ExcelDataSet.Tables[0].Rows.Count; 
       return 1; 

     } 
    } 

我在AjaxCall功能:

function updateProgress() { 
     $.ajax({ 
      type: "POST", 
      url: '<%=ResolveUrl("~/processCalculator.asmx/ShowPercentage") %>', 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      async: true, 
      success: function (msg) { 
       // TODO: revert the line below in your actual code 
       //$("#progressbar").progressbar("option", "value", msg.d); 
       $(".percentage").text(msg.d); 
       if (msg.d < 100) { 
        setTimeout(updateProgress, 100); 
       } 
      } 
     }); 

    } 

我打電話的UpdateProgress按鈕的onclick:

<asp:Button Text="Sorgula" ID="btnAction" class="button_action" OnClick="Action_Click" Width="80px" runat="server" /> 

     $(".button_action").click(function() { 
      var action_ = $(this).attr("value"); 
      var ExcelRowCount = $('#<%=ExcelActionsIsAvaibleRowCount.ClientID %>').val(); 
      if (ExcelRowCount != "") { 
       if (confirm(ExcelRowCount + " kayıt için [" + action_ + "] aksiyonu gerçekleştirilecek?")) { 
        setTimeout(updateProgress, 100); 
        return true; 
       } 
       else 
       { 
        return false; 
       } 
      } 
     }); 

我的Cs動作代碼 保護無效btnAction_Click(對象發件人,EventArgs的){

... 
... 
for (int rowIndex = 1; rowIndex < Glob.ExcelDataSet.Tables[0].Rows.Count; rowIndex++) 
{ 
    ...  
    ...     

    aksiyon_sonucu_ = action.InsertRow(
    Glob.ExcelDataSet.Tables[0].Rows[rowIndex], DegistirilebilirKolonlar, true); 

    // my persentage 
    processCalculater pCal = new processCalculater(); 
    pCal.SetCommittedCount(rowIndex); 

    ... 
    ... 

}

回答

1

您需要使用PageAsyncTask(或其他一些多線程)。

這裏是關於MSDN使用的文章,它也有一個顯示進度指示器的例子。

http://msdn.microsoft.com/en-us/library/system.web.ui.pageasynctask.aspx

對多線程的其他選項是:

ThreadPool.QueueUserWorkItem(s => System.Threading.Thread.Sleep(5000)) 

new Thread(() => System.Threading.Thread.Sleep(5000)){ IsBackground = true }.Start() 

的想法是把你的長期運行的任務到後臺線程(使用上述任何技術)。通過你的上下文(會話)來保存你的進度。然後使用AJAX和WebMethod,可以訪問會話數據並顯示後臺線程的進度。

+0

thx我在嘗試 – Mennan

+0

我怎麼能打電話給這個PageAsyncTask?它只有一個電話我無法想象它。我想每秒鐘打電話給我,我該怎麼做? – Mennan

+0

您不希望每秒鐘只調用一次PageAsyncTask。在我的答案中查看添加的註釋。 –