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();
    }

但它显示tdSectionsAdd中的控件计数为 0,这些控件是在我尝试访问它们之前添加的,但它仍然在 td 中显示没有控件。请帮忙,我怎样才能得到这些文本框?

谢谢!

4

2 回答 2

2

您需要在每个回发中添加它们。将totalSections变量存储在 ViewState 中,以便您也可以在页面加载时添加它们:

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();
}
于 2012-10-29T15:14:34.593 回答
1

动态创建的控件在回发时“消失”,如果它们没有在该页面的 Page_Init 中“重新创建”。

只有在 page_init 中创建它们时,页面的视图状态才会使用它们的信息进行更新。

长解释: 当我们执行回发(或部分回发)时,我们希望能够访问这些控件(或至少是用户放入其中的值)。我们知道数据在 viewstate 中,但是 ASP.NET 并不真正知道 ViewState 项属于哪个控件。它只知道通过相同的索引来匹配视图状态项和控件(例如,将视图状态树中的项目 n 与控件树中的项目 n 匹配)。因此,为了获取动态控件的数据,我们需要在每次回发页面时重新创建控件。但是为了让它工作,我们需要在 Page_Init 函数中而不是在 Page_Load 中重新创建控件。为什么?因为当创建 ViewState 时,它​​需要所有控件都已经存在。 页面生命周期

这取自 MSDN,您可以看到视图状态是在 init 之后但在页面加载之前加载的。

TL;DR调用在 page_init 中创建动态控件的函数,您应该能够看到用户在页面回发时输入的所有值

关于这个问题的几个链接:

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

ASP.NET - 在 Page_Pre_init() 或 Page_Init() 或 Page_Load() 中创建的动态控件

选项 2:

我应该注意:如果控件都具有唯一的 ID,并且您对每次回发都重新创建它们不感兴趣- 您总是可以在请求对象中查找它们。Request.Form 是一个 NameValueCollection,它保存了作为表单一部分的所有控件的值,只需搜索它以查找您要查找的任何内容

于 2012-10-29T15:10:35.630 回答