循环通过文本框名称 [英] looping thru textbox names

查看:73
本文介绍了循环通过文本框名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在这个场景中帮助编写C#:



我有文本框:textbox1,textbox2,textbox3,... textbox8

i希望使用循环将这些文本框中的每个值/字符串保存到数据库。



感谢

i just want a help in coding C# with this scenario:

I have textboxes: textbox1, textbox2, textbox3, ... textbox8
i want to save each values / strings inside these textboxes to database using a loop.

thanks

推荐答案

使用如下代码...



Use code like below...

<pre lang="c#">

for (int i = 1; i< = 8; i ++)

{

TextBox txtbox;

txtbox =(TextBox)this.FindControl (textbox+ i.ToString());

//编写保存值的代码

}

for (int i = 1; i <= 8;i++ )
{
TextBox txtbox;
txtbox = (TextBox)this.FindControl("textbox" + i.ToString());
//write your code for save values
}


当你使用反射可以做到这一点,这是一个繁琐而缓慢的方法。你可能最好不要删除表单上的所有控件,识别TextBoxes,或者设置一个你感兴趣的TextBoxes数组,并将其删除。



这真的取决于你需要做什么:你希望教科书内容转到同一行,那么这是一回事,如果你想要它们不同的行,那就是另一行了。
While you can do exactly that using reflection, it's a cumbersome and slow way to do it. You would probably be better off either lopping through all controls on your form and identifying TextBoxes, or setting up an array of TextBoxes you are interested in and lopping through that.

It really depends on what you need to do: of you want the textbooks contents to go to the same row, then that is one thing, if you want them in different rows, then that is quite another.


你可以循环浏览

you can loop through
Form.Controls

并检查控件的类型

.GetType().FullName == "System.Web.UI.TextBox"

,然后取值。


这篇关于循环通过文本框名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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