2012-10-29 66 views
2

正在創建的文本改變事件;後端一些文本框控件,就像這樣:獲取其上運行時產生

protected void txtHowMany_TextChanged(object sender, EventArgs e) 
    { 
      int totalSections = Convert.ToInt32(txtHowMany.Text.Trim()); 

      for (int i = 1; i <= totalSections; i++) 
      { 
       TextBox tbx = new TextBox(); 
       tbx.Text = ""; 
       tbx.ID = "section" + i; 
       tbx.Style.Add("width", "90%"); 
       tdSectionsAdd.Controls.Add(tbx); 
      } 
      trSectionsName.Visible = true; 
    } 

自動回發是真實的txtHowMany,所以當我輸入一個號碼,它生成的文本框,並把它添加到表師

現在的問題是,我試圖從產生的文本框獲取文字是這樣的:

protected void btnSave_click(object sender, EventArgs e) 
    { 
     int numbersOfSectionsToSave = 1; 
       int sectionsToSave =Convert.ToInt32(txtHowMany.Text.Trim()); 

       for (int i = 1; i < sectionsToSave; i++) 
       { 
        Sections section = new Sections(); 
        section.CourseId = result; 
        section.OrganizationId = course.OrganizationId; 

        foreach (Control c in tdSectionsAdd.Controls) 
        { 
         if (c.GetType() == typeof(TextBox)) 
         { 
          TextBox txtBox = (TextBox)c; 
          string id = "section" + i; 
          if (txtBox.ID == id) 
          { 
           section.Name = txtBox.Text.Trim(); 
          } 
         } 
        } 

        string name = Request.Form["section1"]; 
        section.CreatedBy = "Admin"; 
        section.CreationDate = DateTime.Now; 
        section.ModifiedBy = "Admin"; 
        section.ModificationDate = DateTime.Now; 
        numbersOfSectionsToSave += section.SaveSection(); 
    } 

但是它顯示0計數的控件在tdSectionsAdd,控件在我試圖訪問它們之前被添加,但它仍然顯示td中沒有控件。 請幫忙,我怎樣才能得到這些文本框?

謝謝!

+0

既然你知道的所有文本框的名稱,你爲什麼不嘗試tdSectionsAdd.FindControl呢? – AntLaC

+0

我試過了,但是它返回'null' ...這是我想到的第一個想法。 – ygssoni

回答

2

您需要在每個回發中添加它們。存儲在ViewState中的totalSections變量,所以你可以將它們添加我的頁面加載也:

protected void AddTextBoxes() 
{ 
    int totalSections; 
    if (int.TryParse(Convert.ToString(ViewState["TotalSections"]), out totalSections) 
    { 
     for (int i = 1; i <= totalSections; i++) 
      { 
       TextBox tbx = new TextBox(); 
       tbx.Text = ""; 
       tbx.ID = "section" + i; 
       tbx.Style.Add("width", "90%"); 
       tdSectionsAdd.Controls.Add(tbx); 
      } 
     trSectionsName.Visible = true; 
    } 
} 
protected void txtHowMany_TextChanged(object sender, EventArgs e) 
    { 
      ViewState["TotalSections"] = Convert.ToInt32(txtHowMany.Text.Trim()); 

      tdSectionsAdd.Controls.Clear(); 
      AddTextBoxes(); 
    } 

protected void Page_Load(object sender, EventArgs e) 
{ 
    AddTextBoxes(); 
} 
+0

完美,謝謝! :) – ygssoni

1

動態創建的控件在回發時「消失」,如果它們沒有在該頁面的Page_Init中「重新創建」。

只有在page_init中創建它們,頁面的viewstate纔會更新其信息。

龍Explantion: 當我們執行回發(或部分回發),我們希望能夠訪問這些控件(或至少值用戶放入其中)。 我們知道數據處於視圖狀態,但ASP.NET並不知道ViewState項目屬於哪個控件。它只知道通過相同的索引匹配視圖狀態項和控件(例如,將視圖狀態樹中的項目n匹配到控制樹中的項目n)。因此,爲了獲取動態控件的數據,我們需要在每次頁面回發時重新創建控件。 但是爲了使其工作,我們需要在Page_Load中的Page_Init函數中重新創建控件。 爲什麼?因爲當ViewState被創建時,它需要所有的控件已經存在。 Page Life Cycle

這是從MSDN取得的,因爲您可以看到viewstate在init之後但在頁面加載之前加載。

TL; DR呼叫,在page_init創建動態控件,你應該能夠看到用戶輸入的所有值的功能,當頁面回傳

在這個問題上有幾個環節:

http://forums.asp.net/t/1186195.aspx/1

ASP.NET - Dynamic controls created in Page_Pre_init() or Page_Init() or Page_Load()

選項2:

我應該注意:如果控件都有唯一的ID和你不感興趣的重新建立他們再次回發的每 - 你總是可以尋找他們的請求對象。 Request.Form是一個NameValueCollection,它包含所有屬於表單一部分的控件的值,只需搜索它就可以搜索任何內容

+0

我不知道爲什麼我忘了:) – AntLaC

+0

但是,文本框是在回發後創建 – AntLaC