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

时间:2013-11-23 16:56:39

标签: c# asp.net

我动态创建了一些文本框。单击一个按钮后会创建它们(文本框的数量取决于用户)。

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

    }

现在我需要获取键入该文本框的文本。我尝试过在互联网上找到的东西,但无法让它起作用。

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

欢迎任何形式的帮助。如果您需要更多信息,我会给他们

2 个答案:

答案 0 :(得分:1)

函数中声明的变量仅在函数中可见。您需要将TextBox存储在变量中,即使函数中的代码已“完成”,该变量也存在。有关更多信息,请搜索scopes

这是一个小样本,它将TextBox存储在类中可见的List中。

另一种选择是使用事件处理程序。这取决于您的情况,哪种解决方案更适合。如果将TextBox存储在List中,则可以轻松执行清理代码(例如,如果需要,可以删除EventHandlers)。你可以明显地结合方法1和2.在这种情况下,你将创建的TextBox存储在List(或任何其他集合)中,但你仍然会使用eventhandler中的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
    }
}

答案 1 :(得分:0)

你以同样的方式添加文本框,这是我的例子(对不起它是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)

然后你遍历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#示例

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

}