【问题标题】:Multiple Textbox values into database多个文本框值进入数据库
【发布时间】:2012-01-23 21:10:01
【问题描述】:

我的表单中有多个文本框,例如 txtTask0、txtTask1... txtTask12。

所以我想将这些文本框的值一一传递到我的网络服务中。

for (int i = 0; i <= 12 ; i++)
{
   sOUT = ws_service.InsertAchievement(i,txtTask0.Text,txtAchieve0.Text);              
}

这里我需要像传递“i”值一样,而不是传递 txtTask0.text

txtTask[i].text

类似的东西

TextBox tb = (TextBox) Controls["txtTask" + i];

来自这个link 但是该代码会导致错误,例如

Error   92  The best overloaded method match for 'System.Web.UI.ControlCollection.this[int]' has some invalid arguments 

如何将多个文本框值传递到循环中?

【问题讨论】:

    标签: asp.net c#-2.0


    【解决方案1】:

    你不能那样做,因为 Controls[index] 需要一个整数作为它的参数,但是你传递一个“与整数连接的字符串”它不会工作,而是你像下面那样工作,希望它会帮你...

                foreach (Control c in this.Controls)
                {
                    int i = 0;
                    if (c is TextBox)
                    {
                        while(i < 10)
                        {
                            if (c.Name == "txtTask" + i)
                            {
                                MessageBox.Show("This is textBox" + i);
                            }
                            i++;
                        }
                    }
                }
    

    编辑:

    如果条件if(c is TextBox) 解析不正确,那么就做喜欢

               foreach (Control c in this.Controls)
                {
                    int i = 0;
                    while (i < this.Controls.Count)
                    {
                        if (c.Name == "txtTask" + i)
                        {
                            MessageBox.Show("This is textBox" + i);
                        }
                        i++;
                    }
                }
    

    编辑 2:

    或者,如果您想在 aspx 页面中的所有文本框控件中循环,请使用以下代码部分。它工作得非常完美..

            int count = 0;
            foreach (Control c in this.Page.Controls)
            {
                foreach (Control c1 in c.Controls)
                {
                    int i = 0;
                    if (c1 is TextBox)
                    {
                        while (i < 10)
                        {
                            if (c1.ID == "TextBox" + i)
                            {
                                count++;
                            }
                            i++;
                        }
                    }
                }
            }
            Label1.Text = count + " textbox(es) has been found";
    

    【讨论】:

    • 对不起。上面的代码没有通过if (c is TextBox) 语句以及如何在这里获取文本框的值。因为找不到 c.Name 属性。我用 c.ID 试过了。
    • Control c 拥有LiteralControlHtmlForm 等控件,它不拥有TextBox 控件。我错过了什么吗?
    • 如果项目中没有文本框控件,则仅与控件名称进行比较。注释if(c is TextBox) 行并再次测试。
    • 我的表单中有许多 TextBox 控件。但代码根本没有读取除 LiteralControl 和 HtmlForm 之外的任何控件。
    • String txtOption; TextBox txtTask = new TextBox(); for (int i = 0; i &lt;= 7; i++) { txtOption = "txtTask" + i; txtTask.Text = ((TextBox)FindControl(txtOption)).Text; Response.Write(txtTask.Text); } 我试过这段代码。这是工作。而且您的代码仍然没有进入if 语句,因为循环没有找到 TextBox 控件。我不知道问题出在哪里。感谢您的即时帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多