【问题标题】:Get control which is generated on runtime获取在运行时生成的控制
【发布时间】:2012-10-29 15:05:11
【问题描述】:

我正在通过后端在文本更改事件上创建一些文本框,如下所示:

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 中显示没有控件。 请帮忙,我怎样才能得到这些文本框?

谢谢!

【问题讨论】:

  • 既然知道所有文本框的名称,不如试试 tdSectionsAdd.FindControl 吧?
  • 我试过但它返回null ...这是我想到的第一个想法。

标签: asp.net controls runtime


【解决方案1】:

您需要在每个回发中添加它们。将 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();
}

【讨论】:

    【解决方案2】:

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

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

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

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

    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,它保存了作为表单一部分的所有控件的值,只需搜索它以查找您要查找的任何内容

    【讨论】:

    • 我不知道为什么我忘记了 :)
    • 但是,文本框是在回发之后创建的
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多