在C#/ ASP.NET控件数组类型的循环 [英] Control array type loop in C#/ASP.NET

查看:132
本文介绍了在C#/ ASP.NET控件数组类型的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个C#/ ASP.Net Web应用程序,我有大量的需要在code设置为变量值后面的文本框中。目前,我做以下内容:

I am writing a C#/ASP.Net web application and I have a large number of text boxes that need to be set to variable values in the code behind. Currently I am doing the following:

AspTextBox0.Text = codeBehindVariable[0];
AspTextBox1.Text = codeBehindVariable[1];
AspTextBox2.Text = codeBehindVariable[2];
AspTextBox3.Text = codeBehindVariable[3];
… 

有没有一种简单的方法来做到这一点的一个简单的for循环??

Is there an easy way to do this in a simple "for" loop??

这是一个非常简单的例子中,实际程序有一个开关壳体,并且需要在变量分配的时间将要执行一些其它的测试。因此,for循环将大大简化code的写作和可维护性。早在VB6和控制阵列的好老日子,这是小菜一碟。

This is a very simplified example, the real program has a switch case and some other testing that needs to be performed at the time the variable is assigned. Therefore, a "for" loop would drastically simplify the writing and maintainability of the code. Back in the good-old-days of VB6 and control arrays this was a piece of cake.

推荐答案

VB6的好日子的已经一去不复返了,更好不回来。

The good old days of VB6 are long gone and better don't come back.

创建控件数组或更高的列表与LT;文本框> 自己:

Create a control array or better a List<TextBox> yourself:

var textBoxes = new List<TextBox> {
    AspTextBox0,
    AspTextBox1,
    // ...
};

邮编 它与 codeBehindVariable

textBoxes.Zip(codeBehindVariable, 
              (textBox, variable) => textBox.Text = variable);

或者,如果你preFER一个循环:

for ( int i = 0; i < codeBehindVariable.Length; i++ )
{
    textBoxes[i].Text = codeBehindVariable[i];
} 

请,在循环,你将不得不确保两个文本框 codeBehindVariable 有相同数量的项目(或使循环只运行在条目列表最短的金额)。在邮编函数会照顾这本身。

Keep in mind that in the for loop you will have to make sure that both textBoxes and codeBehindVariable have the same number of items (or make the loop run only over the shortest list's amount of entries). The Zip function will take care of this by itself.

这篇关于在C#/ ASP.NET控件数组类型的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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