正在創建的文本改變事件;後端一些文本框控件,就像這樣:獲取其上運行時產生
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中沒有控件。 請幫忙,我怎樣才能得到這些文本框?
謝謝!
既然你知道的所有文本框的名稱,你爲什麼不嘗試tdSectionsAdd.FindControl呢? – AntLaC
我試過了,但是它返回'null' ...這是我想到的第一個想法。 – ygssoni