Hello

I have an array of 50 text strings, is there any way that i can easily link each item in the array to a different text box on the screen? and then to have the text boxes auto update if the array changes?

Thanks Simon

Dani AI

Generated

For a web page the two practical, maintainable patterns are (A) data-binding to a templated control (Repeater/ListView/GridView) or (B) creating the inputs dynamically and keeping the underlying string collection in server state. Both scale to 50 items; which to choose depends on whether you want a declarative template (easier) or full runtime control. This builds on points from and but gives concrete WebForms guidance.

Use a Repeater/ListView when you want simple generation and easy rebinding. Bind a List(Of String) and rebind whenever the array changes. On postback, iterate the Repeater items to collect edited values and write them back to your collection before rebind.

Example markup:

<asp:Repeater ID="Repeater1" runat="server">
  <ItemTemplate>
    <asp:TextBox ID="txtValue" runat="server" Text='<%# Container.DataItem %>' />
  </ItemTemplate>
</asp:Repeater>

Example code-behind (VB.NET):

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  If Not IsPostBack Then
    Dim items As New List(Of String) From {"a","b","c"} ' replace with your 50 strings
    ViewState("Strings") = items
    Repeater1.DataSource = items
    Repeater1.DataBind()
  End If
End Sub

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
  Dim updated As New List(Of String)
  For Each it As RepeaterItem In Repeater1.Items
    If it.ItemType = RepeaterItemType.Item Or it.ItemType = RepeaterItemType.AlternatingItem Then
      Dim tb As TextBox = CType(it.FindControl("txtValue"), TextBox)
      updated.Add(tb.Text)
    End If
  Next
  ViewState("Strings") = updated
  Repeater1.DataSource = updated
  Repeater1.DataBind()
End Sub

If you must create controls dynamically, recreate them on every request in OnInit (same IDs) so ViewState can restore values. Do not reset their Text on postback (only set initial values when Not IsPostBack) or you will overwrite restored user input. Finally, if you need instant browser updates when the server array changes without a reload, use AJAX polling or SignalR to push changes.

Recommended Answers

All 2 Replies

Declare an array of objects of TextBox class.

Hello

I have an array of 50 text strings, is there any way that i can easily link each item in the array to a different text box on the screen? and then to have the text boxes auto update if the array changes?

Thanks Simon

I know that you could easily do it in vb6, but am not sure about .net
but i am sure that there is a way of setting textboxes up in arrays. And you could set up a routine for when the string changes set textbox array to the string array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.