2013-11-23 76 views
0

我动态创建了一些文本框。它们是在单击一个按钮后创建的(文本框的数量取决于用户)。如何从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++) 
     { 
      *************************************** 
     } 
    } 

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

回答

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) 

然后你通过控制在占位符迭代(在我的例子):

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

} 
+0

ChrisK在他的回答中是正确的,除非您可以设法让他们回到页面,否则每次回发都会失去添加控件。我忘了提到这一点。 – Iztoksson

1

在FUNC声明的变量只在一个函数中可见。您需要将文本框存储在一个变量中,即使函数中的代码已“完成”,该变量仍然存在。有关更多信息,请搜索scopes

这是一个小样本,它将TextBoxes存储在列表中,该列表在您的班级中可见。

另一种选择是使用事件处理程序。这取决于你的情况,哪种解决方案更适合。如果将文本框存储在列表中,则可以轻松执行清理代码(例如,如果需要,请移除EventHandlers)。您可以明显地将方法1和方法2结合在一起。在这种情况下,您会将创建的文本框存储在列表(或任何其他集合)中,但仍然可以在事件处理程序中使用sender以获取对发送文本框的引用。

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 
    } 
} 
+0

我在开始时创建了一个列表,添加了在列表中创建的文本框,然后在foreach中尝试了 Label4.Text = t.Text; 只是为了看它是否工作,但标签的文本没有变化 – user2516578

+0

这很奇怪......你可以发布你的代码缩减示例,显示问题?问题中的 – ChrisK

+0

是代码的第一部分,我在其中创建文本框。我刚刚添加了textBoxes.Add(createdTextbox); 然后我只是添加了foreach(TextBox中的TextBox t) { Label4.Text = t.Text; } – user2516578

相关问题