如何根据 asp.net Web 表单中数据库 table 中的行动态创建文本框?

How to Create textboxes dynamically based on rows in table of a database in asp.net web form?

我想根据 sql 数据库的 table 行数生成相同数量的标签和文本框。 它将生成相同数量的标签和文本框,并将 1 列数据检索到标签The image shows a sample of database table and a webform design

看到这个:

<% var data=(from e in table select e).ToList<type>(); %>
<% foreach(type dr in data)
{ %>
  <label><%=dr.Name %> </label>
  <input type="text" name="<%=dr.ItemID %>" />
<% } %>

您可以使用 Repeater 或 ListView 控件动态创建标签和文本框:

.aspx:

<table>
    <asp:ListView id="lvSample" runat="server">
        <ItemTemplate>
            <tr>
                <td><%# Eval("ItemID") %></td>
                <td><asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />
            </tr>
        </ItemTemplate>
    </asp:ListView>
</table>

然后从代码隐藏中设置 DataSource 值。您也可以使用 DataSource 控件从 .aspx 中设置它。