访问动态创建的文本框文本 [英] Accessing dynamically created textboxes text

查看:149
本文介绍了访问动态创建的文本框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的表单中,最终用户选择了一些动态创建的文本框,这一切都适用于以下代码:

  protected void txtAmountSubmit_Click(object sender,EventArgs e)
{
int amountOfTasks;
int.TryParse(txtAmountOfTasks.Text,out amountOfTasks); (int i = 0; i< amountOfTasks; i ++)
{
TextBox txtAddItem = new TextBox();
txtAddItem.ID =txtAddItem+ i;
txtAddItem.TextMode = TextBoxMode.MultiLine;
questionNine.Controls.Add(txtAddItem);
txtList.Add(txtAddItem.ID);
}
}

但是这也给我带来了一个小问题,稍后在我的表单上提交按钮点击,我将结果发送到需要去的指定人员(使用smtp电子邮件)。再次,这部分很好,直到我试图从这些动态创建的文本框中检索文本



/ strong>


  1. 我尝试使用这个msdn访问服务器控件ID方法,但这不起作用。


  2. 我尝试将这些新的文本框添加到列表中,但是我不确定如何在文本框中添加文本时更新这些文本框。因此,我的结果因为这个原因而返回null。


  3. 我还查看了其他有关SO的问题,例如这个,但是它们通常用于WPF或winforms,而不是asp.net的问题(这通常不是问题,但我不需要从我的页面中的每个文本框控件中获取文本,只是从动态创建的文本框控件中获取文本。

我也试过改变我如何调用我希望工作的代码:

  string textboxesText = string.Join( \\\
,txtList.Select(x => x).ToArray());

然后在我的连接字符串(电子邮件正文)中我会调用:

  textboxesText 

问题



由于它们是动态创建的,我发现很难通过他们的id来调用它们,例如:txtExampleID.Text,也是因为我必须增加ID每次都有一个(所以他们不会相互重叠),这使我的事情有点困难。



我不是要求一个代码解决方案,更喜欢指针正确的方向,因为我还在学习。



所以总结一下:我需要从动态创建的文本框中获取文本,将其添加到我的电子邮件正文中。 p>

解决方案

问题是这些文本框需要在 Load c $ c>事件的页面,每一次,以便事件和值可以挂钩备份和检索。



我认为最直接的方法,在你的情况下,将扩展你已经尝试过的想法#1。在加载中,使用足够的信息创建 列表 重新创建需要在 ViewState Session 中存储列表

  ViewState [DynamicControls] = list; 

  Session [DynamicControls] = list; 

如果可以,我将使用 ViewState 当用户离开页面时,它将被破坏。


I have stumbled across a problem with my asp.net form.

Within my form the end user chooses a number of textboxes to be dynamically created, this all works fine with the following code:

protected void txtAmountSubmit_Click(object sender, EventArgs e)
    {
        int amountOfTasks;
        int.TryParse(txtAmountOfTasks.Text, out amountOfTasks);
        for (int i = 0; i < amountOfTasks; i++)
        {
            TextBox txtAddItem = new TextBox();
            txtAddItem.ID = "txtAddItem" + i;
            txtAddItem.TextMode = TextBoxMode.MultiLine;
            questionNine.Controls.Add(txtAddItem);
            txtList.Add(txtAddItem.ID);
        }
    }

However this has also caused a small problem for me, later on in my form on the submit button click, I send the results to the specified person it needs to go to (using smtp email). Again this part is fine, until I am trying to retrieve the text from these dynamically created textboxes.

What I have tried

  1. I have tried using this msdn access server controls ID method however this was not working.

  2. I tried to add these new textboxes to a list, however I was unsure on how to update these textboxes when they have text in them. Therefore my results were returning null because of this.

  3. I have also looked at other questions on SO such as this however they are usually for WPF or winforms, rather than my problem with asp.net (this usually isn't an issue, but I don't need to get the text from every textbox control in my page, just the ones that were dynamically created).

I have also tried changing how I call the code that I hoped would have worked:

string textboxesText = string.Join("\n", txtList.Select(x => x).ToArray());

and then in my concatenated string (email body) I would call:

textboxesText

The problem

As they are dynamically created I am finding it difficult to call them by their id for example: txtExampleID.Text, also as I have to increment the ID's by one each time (so they don't override each other) it has made things a little bit more difficult for me.

I am not asking for a code solution, I would prefer pointers in the right direction as I am still learning.

So to sum it all up: I need to get the text from my dynamically created textboxes to add it to my email body.

解决方案

The issue is these text boxes need recreated in the Load event of the page, every single time, so that both events and values can be hooked back up and retrieved.

I think the most straight forward approach, in your case, would be to extend idea #1 that you had already tried. Build a List of these controls with enough information to recreate them in Load, but you need to store that List in either ViewState or Session.

ViewState["DynamicControls"] = list;

or

Session["DynamicControls"] = list;

I would use ViewState if you can because it gets destroyed when the user leaves the page.

这篇关于访问动态创建的文本框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆