locked
Display Array List Value to Multiple Textbox RRS feed

  • Question

  • User1228272764 posted

    i have 12 textbox that named in sequence like TxtTglCuti1, TxtTglCuti2 and so on until 12.

    and in serverside, i have array list : 

     Public ListTgl As New List(Of String)()

    i want to assign all the array list value to the textboxt programaticaly by loop with pre-define condition like : 

            For i As Integer = 0 To TxtDetailJmlPengajuan.Text - 1
               TxtTglCuti(i).text = listTgl(i)
            Next

    my problem is, how we define the i value for textboxt loop ? my textbox start from 1 and the loop start from 0 to reach correct array list index. may be you can suggest me some answer to fix this problem ? 

    my goals : display arraylist value with appropriate textbox based on loop. (ex: i have 4 value inside the array list, so only 4 textboxes will display in web form with approproate value from array list. 

    especialy, its code in VB.net+ASP.NET :)

    Tuesday, October 16, 2018 9:36 AM

Answers

  • User-369506445 posted

    hi

    you can try below code

    Public ListTgl As New List(Of String)()
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ListTgl.Add("ListTgl1")
            ListTgl.Add("ListTgl2")
            ListTgl.Add("ListTgl3")
            ListTgl.Add("ListTgl4")
            ListTgl.Add("ListTgl5")
            ListTgl.Add("ListTgl6")
            ListTgl.Add("ListTgl7")
            ListTgl.Add("ListTgl8")
            ListTgl.Add("ListTgl9")
            ListTgl.Add("ListTgl10")
            ListTgl.Add("ListTgl11")
            ListTgl.Add("ListTgl12")
    
    
            For index = 1 To ListTgl.Count
                Dim textbox As TextBox = Me.FindControl("TxtTglCuti" & index )
                textbox.Text = ListTgl(index - 1)
            Next
    
        End Sub

    Html

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <form runat="server">
            <asp:TextBox ID="TxtTglCuti1" runat="server" />
            <asp:TextBox ID="TxtTglCuti2" runat="server" />
            <asp:TextBox ID="TxtTglCuti3" runat="server" />
            <asp:TextBox ID="TxtTglCuti4" runat="server" />
            <asp:TextBox ID="TxtTglCuti5" runat="server" />
            <asp:TextBox ID="TxtTglCuti6" runat="server" />
            <asp:TextBox ID="TxtTglCuti7" runat="server" />
            <asp:TextBox ID="TxtTglCuti8" runat="server" />
            <asp:TextBox ID="TxtTglCuti9" runat="server" />
            <asp:TextBox ID="TxtTglCuti10" runat="server" />
            <asp:TextBox ID="TxtTglCuti11" runat="server" />
            <asp:TextBox ID="TxtTglCuti12" runat="server" />
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 10:23 AM

All replies

  • User-369506445 posted

    hi

    you can try below code

    Public ListTgl As New List(Of String)()
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ListTgl.Add("ListTgl1")
            ListTgl.Add("ListTgl2")
            ListTgl.Add("ListTgl3")
            ListTgl.Add("ListTgl4")
            ListTgl.Add("ListTgl5")
            ListTgl.Add("ListTgl6")
            ListTgl.Add("ListTgl7")
            ListTgl.Add("ListTgl8")
            ListTgl.Add("ListTgl9")
            ListTgl.Add("ListTgl10")
            ListTgl.Add("ListTgl11")
            ListTgl.Add("ListTgl12")
    
    
            For index = 1 To ListTgl.Count
                Dim textbox As TextBox = Me.FindControl("TxtTglCuti" & index )
                textbox.Text = ListTgl(index - 1)
            Next
    
        End Sub

    Html

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <form runat="server">
            <asp:TextBox ID="TxtTglCuti1" runat="server" />
            <asp:TextBox ID="TxtTglCuti2" runat="server" />
            <asp:TextBox ID="TxtTglCuti3" runat="server" />
            <asp:TextBox ID="TxtTglCuti4" runat="server" />
            <asp:TextBox ID="TxtTglCuti5" runat="server" />
            <asp:TextBox ID="TxtTglCuti6" runat="server" />
            <asp:TextBox ID="TxtTglCuti7" runat="server" />
            <asp:TextBox ID="TxtTglCuti8" runat="server" />
            <asp:TextBox ID="TxtTglCuti9" runat="server" />
            <asp:TextBox ID="TxtTglCuti10" runat="server" />
            <asp:TextBox ID="TxtTglCuti11" runat="server" />
            <asp:TextBox ID="TxtTglCuti12" runat="server" />
        </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 10:23 AM
  • User1228272764 posted

    hi vahid, thank you for the answer. but it's still got an error like : 

    Object reference not set to an instance of an object.

    on the line of code : 

    textbox.Text = ListTgl(index - 1)

    by this error i know the main problem is i am trying to use a null object as if it was a properly referenced object. But i think it will be eliminate null object value if we try to display it inside loop ? it will only show textbox with the value related.

    Wednesday, October 17, 2018 1:45 AM
  • User-369506445 posted

    did you try my code the same?

    I tried my code and it works, please put your complete code here

    Wednesday, October 17, 2018 5:01 AM
  • User-893317190 posted

    Hi tandasanyu,

    From  your code  TxtTglCuti(i), it seems that you have a list of textbox.

    It's not clear about your code TxtDetailJmlPengajuan.Text - 1, so I use 12 as default.

    Below is my code.

     Public ListTgl As New List(Of String)()
        Public TxtTglCuti As New List(Of TextBox)()
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            For index = 1 To 12
                TxtTglCuti.Add(New TextBox())
            Next
    
    
            ListTgl.Add("the first content")
            ListTgl.Add("the second content")
            ListTgl.Add("the third content")
            ListTgl.Add("the forth content")
    
            For index = 0 To ListTgl.Count - 1
                TxtTglCuti(index).Text = ListTgl(index)
                Me.form1.Controls.Add(TxtTglCuti(index))
            Next
    
        End Sub

    The result.

    Best regards,

    Ackerly Xu

    Wednesday, October 17, 2018 8:44 AM
  • User1228272764 posted

    sorry dude, i'm late to mark this as an answer for me. but still big thanks for your answer :)

    Tuesday, November 27, 2018 7:37 AM