2016-01-22 79 views
1

我需要在一個循環中手動輸入一些值。我正在使用C#ASP.Net和WebForms。暫停循環,直到點擊模態按鈕

我想這一點:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace systemII 
{ 
    public partial class MY_System : System.Web.UI.Page 
    { 
     private List<String> List = new List<string>(); 
     private bool loop = false; 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      while (loop) { } 

      for (int i = 1; i <= 10; i++) 
      { 
       string a = "A" + i.ToString(); 
       if (i == 4 || i == 5) 
       { 
        loop = true; 
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true); 
       } 
       else 
       { 
        List.Add(a); 
       } 
      } 
     } 

     protected void Button2_Click(object sender, EventArgs e) // modal button 
     { 
      string b = "BB"; 
      Lista.Add(b); 
      loop = false; 
      return; 
     } 
    } 
} 

但模式循環結束後出現在屏幕上。我需要暫停循環,直到輸入文本框的值。

任何可以幫助我嗎?對不起,我的英語不好。

+0

您當前正在運行同步代碼。考慮使用[異步代碼](https://msdn.microsoft.com/de-de/library/hh191443.aspx)進行暫停並繼續按鈕。否則,循環將首先完成,然後將休息。 –

回答

2

在ASP.Net中,您不能只是暫停服務器端代碼,因爲即使您在ASP.Net中使用異步頁面,並且服務器端連接了never,服務器端代碼也始終完全執行(從不部分)瀏覽器,但在一個單獨的過程中執行。瀏覽器從不知道和不關心服務器端發生的事情,服務器端不知道也不關心瀏覽器在做什麼,so it's impossible將服務器端代碼連接到瀏覽器所需的方式。

但是,當達到第一個索引= 4並且發出模態彈出腳本時,可以通過從循環中斷開來模擬所需的內容。然後你可以爲i = 5做類似的事情,當用戶輸入i = 4的值並且頁面已經回傳時。但是,成功處理輸入的i的值需要跨瀏覽器的請求進行跟蹤,下面的代碼通過設置ViewState變量HandledValues來完成,該變量只是字符串類型列表的集合。

因此,如果您使用此代碼的工作流程將是:用戶將在一個模式彈出提示輸入值,對於i = 4,然後點擊和按鈕Button1當頁面回,用戶將被提示在模態彈出框中輸入i = 5的值。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    //a list of values handled is stored in ViewState as ValuesHandled 
    List<string> handledValues = new List<string>(); 
    if (ViewState["ValuesHandled"] != null) { 
     List<string> handledValues = (ViewState["ValuesHandled"] as List<string>; 
    } 

    for (int i = 1; i <= 10; i++) 
    { 
     string a = "A" + i.ToString(); 
     //check if i = 4 has had its values input successfully 
     if (i == 4 && !handledValues.Contains(i.ToString())) 
     { 
      loop = true; 
      ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true); 
      handledValues.Add(i.toString()); 
      //break the loop since we need to return to browser for input by user 
      break; 
     } 
     else if (i == 4 && handledValues.Contains(i.ToString())) 
     { 
     //remove handled value if no modal popup needed to be opened 
     handledValues.Remove(i.ToString()) 
     } 
     else if (i == 5 && !handledValues.Contains(i.ToString())) 
     {//check if i = 5 has had its values input successfully 
      loop = true; 
      handledValues.Add(i.toString()); 
      ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true); 
      //break the loop since we need to return to browser for input by user 
      break; 
     } 
     else if (i == 5 && handledValues.Contains(i.ToString())) 
     { 
     //remove handled value if no modal popup needed to be opened 
     handledValues.Remove(i.ToString()) 
     } 
     else 
     { 
      List.Add(a); 
     } 
    } 


    //update the ViewState for ValuesHandled 
    ViewState["ValuesHandled"] = handledValues; 
} 
+0

我已經用你的幫助解決了我的問題!非常感謝:) – Bruno

+1

@布魯諾,太棒了。隨時歡迎你。 – Sunil