【问题标题】:Iterate all dynamically generated textBoxes on button click in ASP.NET在 ASP.NET 中单击按钮时迭代所有动态生成的文本框
【发布时间】:2018-02-19 08:47:26
【问题描述】:

单击“下载”按钮时,我需要遍历页面上所有动态生成的文本框。以下代码不返回任何内容:

    protected void Download_Click(object sender, EventArgs e)
    { //???
        foreach (Control child in this.form1.Controls)
        {
            if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")) //child is somehow never textbox
            {
                TextBox textBox = (TextBox)child;
                System.Diagnostics.Debug.WriteLine(textBox.Text);
            }
        }
    }

我通过调用 AddTextbox 函数来创建文本框:

    private void AddTextbox(string id, bool isNumber)
    {
        TextBox textBox = new TextBox();
        textBox.ID = id; //set ID
        if (isNumber){ textBox.Attributes.Add("type", "number"); }; //if only numbers can be added, html attribute 'type = "number"' is added
        form1.Controls.Add(textBox);
        form1.Controls.Add(new LiteralControl("<br />")); //just add <br /> html element            
    }

【问题讨论】:

  • 您是否在表单初始化时重新创建动态创建的文本框?此外,如果 TextBox 不直接位于表单上,则需要迭代控件的子级。
  • 不,我想我不是。我用一个函数创建它们,然后用第二个函数迭代它们以填充值,然后我试图下载它们,如this question所示。我可以要求您提供一些参考或教程,基于这些我将能够做到这一点吗? >第二个问题:是的,你是对的,我也需要解决这个问题。
  • 除了诊断,你调试吗?
  • 是的。 child 永远不是文本框,if 语句中的断点甚至不会触发一次。
  • 其实我不清楚你想要实现什么。 :(

标签: c# asp.net .net webforms


【解决方案1】:

以下是您想要实现的完整工作示例:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
    <asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddTextBox" />
    <asp:Button ID="btnGet" runat="server" Text="Download" OnClick="Download_Click" />

    </div>
    </form>
</body>
</html>

C#

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

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
            int i = 1;
            foreach (string key in keys)
            {
                this.CreateTextBox("txtDynamic" + i);
                i++;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void AddTextBox(object sender, EventArgs e)
        {
            int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
            this.CreateTextBox("txtDynamic" + index);
        }

        private void CreateTextBox(string id)
        {
            TextBox txt = new TextBox();
            txt.ID = id;
            pnlTextBoxes.Controls.Add(txt);

            Literal lt = new Literal();
            lt.Text = "<br />";
            pnlTextBoxes.Controls.Add(lt);
        }

        protected void Download_Click(object sender, EventArgs e)
        { //???
            foreach (Control child in pnlTextBoxes.Controls)
            {
                if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")) //child is somehow never textbox
                {
                    TextBox textBox = (TextBox)child;
                    System.Diagnostics.Debug.WriteLine(textBox.Text);
                }
            }
        }

    }
}

最重要的部分是Page_PreInit,您必须在其中重新创建动态创建的文本框。 如果您有不清楚的地方,请告诉我(代码来自此页面https://www.aspsnippets.com/Articles/Get-Value-Text-of-dynamically-created-TextBox-in-ASPNet-using-C-and-VBNet.aspx

【讨论】:

    【解决方案2】:

    您是否尝试过this.Form.Controls 而不是this.Controlsthis 指的是.aspx 页面,TextBox 通常不是页面的直接子页面,它应该是表单的子页面。

    【讨论】:

    • 是的,不工作。看来我的问题是没有正确创建文本框。
    • 你能发布用于创建文本框的代码吗?
    • 我认为您的代码在 PostBack 之后不会保留 TextBoxes
    • 你需要在每个 PostBack 上重新创建你的 TextBoxes,这个链接解释了如何很好地做到这一点:aspsnippets.com/Articles/…
    【解决方案3】:

    您遇到的问题是,虽然您正在创建要在页面上显示的文本框,但当调用下载按钮单击方法时,它们不再存在。

    当页面最初加载时,您正在创建文本框并呈现包含它们的 html。

    单击下载按钮时,会发生回发。这实际上是调用页面的一个新实例。所以此时还没有创建文本框。

    因此,在您可以访问 form1.Controls 下的文本框之前,您需要先使用最初创建它们的相同方法重新创建它们。但是,它们不会具有可能通过浏览器输入的值。

    不过,此数据应该在发布的表单中,因此您需要修改 AddTextBox 方法以尝试从发布数据中获取数据。比如:

    private void AddTextbox(string id, bool isNumber)
    {
        TextBox textBox = new TextBox();
        textBox.ID = id; //set ID
        if (isNumber){ textBox.Attributes.Add("type", "number"); }; //if only numbers can be added, html attribute 'type = "number"' is added
        if (Page.IsPostBack)
        {
            // set value from passed form data
            textbox.Text = Request.Form[id];
        }
        form1.Controls.Add(textBox);
        form1.Controls.Add(new LiteralControl("<br />")); //just add <br /> html element            
    }
    

    我不确定,但您可能还需要设置 textbox.ClientIdMode = static 以便使用您指定的确切 ID 呈现表单的 html。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 2013-07-09
      • 2018-02-16
      • 1970-01-01
      相关资源
      最近更新 更多