Meysam  Savameri
Meysam Savameri

Reputation: 556

Loop through Textboxes in asp.net and fill with array of string

This is my code

  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

I have 5 textboxes. I want to fill just textbox1 and textbox2 with array value. because no I have to word. but when I run the code "str1" repetition on textbox1,textbox2 and textbox3.

Upvotes: 0

Views: 2638

Answers (2)

Rab
Rab

Reputation: 35572

you need one loop to do it

  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

Upvotes: 1

Dai
Dai

Reputation: 155145

That's because your code runs through elements 0, 1 and 2, which correspond to TextBox1, TextBox2, and TextBox3. If you only want to populate TextBox1 and TextBox2 then remove TextBox3 from the array.

You also have a loop within another loop - I don't see why you're doing that.

Upvotes: 0

Related Questions