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

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

问题描述

我已经跨越了问题绊倒我的asp.net的形式。

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

在我的形式最终用户选择了一些文本框动态创建的,这一切都正常工作与以下code:

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

不过,这也在我的提交按钮点击的形式引起了不小的问题对我来说,以后,我将结果发送到它需要去(使用SMTP电子邮件)指定的人。再次,这部分是好的,,直到我尝试从这些动态创建文本框文本

我试图


  1. 我一直在使用这MSDN接入服务器控件ID的方法然而,这是行不通的。

我想这些新的文本框添加到列表,但是我是如何当他们在他们的文字更新这些文本框不确定。因此,我的结果是因为这个返回null。

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.

我也试图改变我如何调用code,我希望会工作:

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

问题

,因为它们是动态创建的,我发现很难通过它们的ID叫他们,例如:txtExampleID.Text,还因为我有一个每次递增的ID(这样它们就不会互相覆盖)有使事情对我来说更困难一点。

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.

我不要求为code解决方案,我想preFER指针在正确的方向,因为我仍然在学习。

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.

我觉得最直接的方法,你的情况,将是延长的想法#1,你已经尽力了。建立足够的信息,这些控件的列表重新创建加载,但你需要存储列表在任的ViewState 会话

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;

Session["DynamicControls"] = list;

我会使用的ViewState 如果你可以因为它可以让当用户离开页面销毁。

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

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

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