2012-06-08 57 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。在我的答案中查看添加的注释。 –