简体   繁体   English

如何从ASP.NET中动态创建的文本框中获取文本

[英]How to get text from dynamically created textboxes in ASP.NET

I created dynamically some textboxes. 我动态创建了一些文本框。 They are created after click on one button(number of the textboxes depends on the user). 它们是在单击一个按钮后创建的(文本框的数量取决于用户)。

     protected void Button1_Click(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox2.Text);
        Table tbl = new Table();
        tbl.Width = Unit.Percentage(80);
        TableRow tr;
        TableCell tc;
        TextBox txt;
        CheckBox cbk;
        DropDownList ddl;
        Label lbl;
        Button btn;
        for (int j = 1; j <= i; j++)
        {
            tr = new TableRow();
            tc = new TableCell();
            tc.Width = Unit.Percentage(25);
            lbl = new Label();
            lbl.Text = "Pitanje:";
            tc.Controls.Add(lbl);
            tr.Cells.Add(tc);
            tc.Width = Unit.Percentage(25);
            txt = new TextBox();
            txt.ID = "txt_p_" + j;
            tc.Controls.Add(txt);
            tr.Cells.Add(tc);

            tc.Width = Unit.Percentage(25);
            lbl = new Label();
            lbl.Text = "Odgovori:";
            tc.Controls.Add(lbl);
            tr.Cells.Add(tc);
            tc.Width = Unit.Percentage(25);
            txt = new TextBox();
            txt.ID = "txt_o_" + j;
            tc.Controls.Add(txt);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);

        }
        Panel1.Controls.Add(tbl);

    }

now I need to get the text that is typed into that textboxes. 现在,我需要获取在该文本框中键入的文本。 I tried with something that I found on the internet but can't get it to work. 我尝试了一些在互联网上找到的东西,但无法正常工作。

     protected void SpremiPitanja(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox2.Text);
        for (int j = 1; j <= i; j++)
        {
          ***************************************
        }
    }

any kind of help is welcome. 任何形式的帮助都值得欢迎。 if you need more information I will give them 如果您需要更多信息,我会给他们

A variable declared in a function is only visible in a function. 在函数中声明的变量仅在函数中可见。 You need to store the TextBoxes in a variable, that exists even when the code in the function has "finished". 您需要将TextBoxes存储在一个变量中,即使函数中的代码“完成”时该变量也存在。 For more information search for scopes . 有关更多信息,请搜索范围

Here is a small sample that stores TextBoxes in a List that is visible in your class. 这是一个将TextBoxes存储在类中可见的List中的小示例。

Another option would be to use eventhandlers. 另一种选择是使用事件处理程序。 It depends on your scenario, which solution would be suited better. 这取决于您的方案,哪种解决方案更合适。 If you store the TextBoxes in a List, you can easily perform clean up code (for instance remove EventHandlers if required). 如果将文本框存储在列表中,则可以轻松执行清理代码(例如,如果需要,请删除EventHandlers)。 You can obviously combine Approach 1 and 2. In that case you would store the created TextBox in a List (or any other collection), but you would still use the sender in the eventhandler to get a reference to the sending TextBox. 显然,您可以将方法1和2组合在一起。在这种情况下,您可以将创建的TextBox存储在List(或任何其他集合)中,但仍将使用事件处理程序中的sender来获取对发送TextBox的引用。

public partial class Form1 : Form
{
    List<TextBox> textBoxes = new List<TextBox>();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Approach 1: create and add textbox to list
        TextBox createdTextbox = new TextBox();
        textBoxes.Add(createdTextbox);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //use the textboxes from the list
        foreach(TextBox t in textBoxes)
        {
            //do something with t
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        //Approach 2: use eventhandlers and don't store textbox in a list
        TextBox createdTextbox = new TextBox();
        createdTextbox.TextChanged += createdTextbox_TextChanged;
        listBox1.Items.Add(createdTextbox);
    }

    void createdTextbox_TextChanged(object sender, EventArgs e)
    {
        TextBox t = sender as TextBox;

        if (t == null)
            throw new ArgumentException("sender not of type TextBox", "sender");

        //do something with t
    }
}

You add textboxes the same way you do, this is my example (sorry it's vb.net): 您以相同的方式添加文本框,这是我的示例(很抱歉,这是vb.net):

Dim t As New TextBox With {.ID = "txt_123", .Text = "Vpiši"}
PlaceHolder1.Controls.Add(t)
t = New TextBox With {.ID = "txt_456", .Text = "Briši"}
PlaceHolder1.Controls.Add(t)

And then you iterate through controls in Placeholder (in my example): 然后您遍历Placeholder中的控件(在我的示例中):

Dim tItem As TextBox
Dim tValue As String = String.Empty
For Each c As Control In PlaceHolder1.Controls
    If TypeOf c Is TextBox Then
        tItem = c
        tValue = tItem.Text.ToString
    End If
Next

C# example added 添加了C#示例

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TextBox t = new TextBox();
        t.Text = "Vpiši";
        t.ID = "txt_123";
        PlaceHolder1.Controls.Add(t);

        t = new TextBox();
        t.Text = "Briši";
        t.ID = "txt_456";
        PlaceHolder1.Controls.Add(t);
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox tItem;
    String tValue;

    foreach (Control c in PlaceHolder1.Controls)
    {
        if (c.GetType() == typeof(TextBox))
        {
            tItem = (TextBox)c;
            tValue = tItem.Text;
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net:不显示ReportViewer(来自动态创建的文本框的参数) - ASP.net: ReportViewer not displaying (parameters from dynamically created textboxes) 从动态创建的 asp.net 文本框将数据保存到数据库 - Saving Data to the Database from Dynamically created asp.net textboxes 从asp.net中动态创建的文本框中获取文本 - Get text from dynamically created textbox in asp.net 如何从ASP.NET中动态创建的文本框中获取数据 - How to get data from dynamically created text-boxes in ASP.NET 如何从动态创建的文本框中获取文本以存储在数据库中? - How to get text from dynamically created textboxes so as to store in database? asp.net Web应用程序中的文本框控件出现问题。 没有从文本框中获取文本以进行更新 - Problem with Textboxes control in asp.net web application. Didn't get text from textboxes for update ASP.Net从表中动态创建的行中获取信息 - ASP.Net get information from dynamically created rows in a table 从 Gridview 获取动态创建的复选框值 | Asp.net - Get dynamically created checkbox value from Gridview | Asp.net 从动态添加的文本框asp.net c#获取值 - get values from dynamically added textboxes asp.net c# 如何获取动态创建的asp.net GridView的行索引? - How to get the row index of a dynamically created asp.net GridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM