循环遍历asp.net中的Textbox并填充字符串数组

时间:2012-06-24 17:54:03

标签: asp.net vb.net

这是我的代码

  Dim str As String = "str1,str2"
    Dim array() As String = str.Split(",")
    Dim MyListOfTextBoxes() As TextBox = {TextBox1, TextBox2, TextBox3}
    For index = 0 To array.Count - 1
        For i = 0 To MyListOfTextBoxes.Length - 1
            MyListOfTextBoxes(i).Text = array(index)
        Next
    Next

我有5个文本框。我想用数组值填充textbox1和textbox2。因为没有我必须说出来。但是当我在"str1"textbox1textbox2上运行代码textbox3重复时。

2 个答案:

答案 0 :(得分:1)

你需要一个循环才能做到这一点

  Dim str As String = "str1,str2"
    Dim array() As String = str.Split(",")
    Dim MyListOfTextBoxes() As TextBox = {TextBox1, TextBox2, TextBox3}
    For index = 0 To array.Count - 1
       if(MyListOfTextBoxes.Length>index)
       MyListOfTextBoxes(index).Text = array(index)
    Next

答案 1 :(得分:0)

那是因为你的代码遍历元素0,1和2,它们对应于TextBox1,TextBox2和TextBox3。如果您只想填充TextBox1和TextBox2,则从数组中删除TextBox3。

你在另一个循环中也有一个循环 - 我不明白为什么你这样做。

相关问题