将多个文本框值存入数据库

发布于 2024-12-28 13:37:04 字数 599 浏览 0 评论 0原文

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

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

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

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

txtTask[i].text

与此类似的

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

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

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

I have multiple textboxes in my form like txtTask0, txtTask1... txtTask12.

so I want to pass values of those textboxes into my webservice one by one.

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

Here instead of passing txtTask0.text I need to pass the "i" value one by one like

txtTask[i].text

something similar to this

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

from this link
But that code results in error like

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

How can I pass multiple textbox values into the loop.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2025-01-04 13:37:04

你不能这样做,因为 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";

You cant do like that, because the Controls[index] expects an integer as its parameter, but you are passing a "string concatenated with an integer" it wont work, instead you do like below it will work, hope it will help you...

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

EDIT :

If the condition if(c is TextBox) is not parsing correctly then do like

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

EDIT 2:

Or Simply if you want to loop out in all textbox controls in aspx page use this following code part. It works very perfectly..

        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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文