0

I have 10 textbox in my web page. I have 10 latest records from my database.

How do I loop through the textboxes and assign the database records into them?

Currently I am using hardcode method:

For i = 0 To _datatable.Rows.Count
    If i = 0 Then
        txtbox1.Text = _datatable.Rows(0)("shift").ToString
    ElseIf i = 1 Then
        txtbox2.Text = _datatable.Rows(1)("shift").ToString
    ElseIf i = 2 Then
        txtbox3.Text = _datatable.Rows(2)("shift").ToString
    // and so on...
    End If
Next
2
  • so what code have you tried so far? Commented Apr 11, 2017 at 4:18
  • @CurseStacker Please look at my update Commented Apr 11, 2017 at 4:27

2 Answers 2

1

Create a Repeater control

<asp:Repeater ID="rptItems" runat="server">
    <ItemTemplate>
        <asp:ID="txtShift" runat="server" Text='<%#Eval("shift") %>' />
    </ItemTemplate>
</asp:Repeater>

<%Eval("shift") %> is what bounds your TextBox to the DataSource. Anything you put inside the <ItemTemplate> will be repeated for each row in your DataTable.

Then on your code behind

rptItems.DataSource = _datatable
rptItems.DataBind()
Sign up to request clarification or add additional context in comments.

Comments

0
<asp:Panel ID="pnl1" runat="server">
  <asp:TextBox ID="txt1" runat="server"></asp:TextBox><br />
  <asp:TextBox ID="txt2" runat="server"></asp:TextBox><br />
  <asp:TextBox ID="txt3" runat="server"></asp:TextBox><br />
  <asp:TextBox ID="txt4" runat="server"></asp:TextBox><br />
  <asp:TextBox ID="txt5" runat="server"></asp:TextBox><br />
</asp:Panel>

Code Behind:

For i = 1 To dt.rows.count
  Dim _txt As TextBox = pnl1.FindControl("txt" & i.ToString.Trim)
  Dim _row As String = dt.Rows(i)("column name of your datatable").ToString.Trim
  _txt.Text = _row
Next
  • dt = datatable - records from sql

  • hope this code helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.