节约文本框的值在一个循环(asp.net网页) [英] saving values of textbox in a loop (asp.net web page)

查看:110
本文介绍了节约文本框的值在一个循环(asp.net网页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图遵循一些教程和code例子,但事情没有工作,我所期望的。

I've been trying to follow a few tutorials and code examples but something isn't working as I'd expect.

我有一系列网页上的文本框,并通过我每一个需要循环,并保存它的值到数据库中。在页面上的文本框的数量将有所不同。我打开他们的数据库。他们都加入到一个表对象。

I have a series of textboxes on a webpage and I need to loop through each one and save it's value to the database. The number of textboxes on the page will vary. I load them from a database. They are all added to a table object.

TableCell cellb = new TableCell();
TextBox txtAnswer = new TextBox();
txtAnswer.TextMode = TextBoxMode.MultiLine;
txtAnswer.Rows = 2;
txtAnswer.ID = "field_" + dataRow["fieldID"].ToString();
txtAnswer.Text = "answer"; //this will be got from the database

cellb.Controls.Add(txtAnswer);

,这样增加了文本框的表行。然后,我有一个保存按钮,它具有以下code

so that adds the textbox to the table row. I then have a save button which has the following code

foreach (Control c in Page.Controls)
{
    foreach (Control childc in c.Controls) 
    {
        if (childc is TextBox)
        {
            TextBox tmpText = (TextBox)childc;
            tmpField = tmpText.ID.Split('_');
            fieldID = Convert.ToInt32(tmpField[1]);
            //save value to the database (eventually)
            debug.InnerHtml += tmpText.Text; //this just outputs the values for now
        }
    }
}

所以上面应该循环,虽然所有的页面控件和查找文本框上在Page_Load作为补充。不过,我现在不知道是否是因为它们不存在。所以,当我保存的页面,它不知道的控件。我可以看到表的控制,但没有在里面....任何想法?!

So the above should loop though all the page controls and find the textfields as added on the page_load. However, I am now wondering if it's because they don't exist. So when I save the page, it doesn't know of the controls. I can see the table control, but nothing inside it.... any ideas?!

推荐答案

动态控件必须在每个页面请求被加入。 $ P的期间pferably $初始化事件。这听起来像他们尚未通过您通过控制迭代时添加的(再次)。

Dynamic controls must be added on each page request. Preferably during the Init event. It sounds like they have not been added yet (again) by the time you iterate through your controls.

另外,如果你知道你的文本输入框是一个特定的控制范围之内,你可能应该先找到控制,使用您正在使用同样的方法,迭代的控件。两个原因是:效率和也,在你的code,你只搜索两个级别从页面控制下来。这可能是好的,但它包含不包含任何的文本框的其他控件。

Also, if you know your TextBoxes are within a specific control you should probably find that control first and then, using the same approach you are using, iterate over the controls. Two reasons for this are: efficiency and also, on your code, you are only searching two levels down from the page control. This may be ok but it includes other controls that will not contain any of those textboxes.

这篇关于节约文本框的值在一个循环(asp.net网页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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