【问题标题】:how to display data in HTML table in asp.net vb.net如何在asp.net vb.net的HTML表格中显示数据
【发布时间】:2023-04-01 07:21:01
【问题描述】:

我在数据库中有一个名为 user 的表。这些列是id, name, phone and address。我把textbox 作为搜索框,所以当用户使用搜索框搜索时,它会显示相关用户列表。我想检索 HTML table 中的数据列表,因为我想为检索到的每一行添加一个 edit 按钮。我尝试了这段代码,但 ForEach 函数有问题。当我以ForEach 分开时,它说For 必须以Next 结尾

<TABLE style="Z-INDEX: 113; POSITION: absolute; TOP: 368px; LEFT: 592px" id="Table1" border="1" cellSpacing="1" cellPadding="1" width="300">
<% for each(DataRow row in ds.Tables[0].Rows) { %>
  <TR>
     <td><%= row["columnName"] %></td>
   </TR><% } %>
</TABLE>

我已经使用以下代码将我的数据检索到SQLDataAdapter

Dim Adpt As New SqlDataAdapter(cm.CommandText, cnConnect)
Dim ds As New DataSet

【问题讨论】:

  • 我建议你使用GridView

标签: html asp.net vb.net


【解决方案1】:

在 ASP.NET 中,您有 HTML 控件,例如 &lt;asp:panel&gt;&lt;asp:button&gt; 等...其中一个控件可能是最强大(也是最复杂)的 &lt;asp:GridView&gt;,它允许您绑定数据到控件,它将负责显示和编辑。

了解它的最好方法,就是举例,如果你在 youtube 上搜索 asp gridview,你会发现几个......试试看,这样你就可以做类似的事情:

<asp:GridView ID="my_gv" Runat="server"
              DataSourceID="myDataSource" DataKeyNames="id"
              AutoGenerateColumns="True"
              AllowPaging="True" />

这个控件有很多东西可以使用,尝试使用 Visual Studio UI,因为它可以让你选择一些选项...

(图片来自web article

【讨论】:

    【解决方案2】:

    在 aspx 页面(表单)中使用此代码

    <asp:Repeater ID="rptCustomers" runat="server">
        <HeaderTemplate>
            <table cellspacing="0" rules="all" border="1">
                <tr>
                    <th scope="col" style="width: 80px">
                        Customer Id
                    </th>
                    <th scope="col" style="width: 120px">
                        Customer Name
                    </th>
                    <th scope="col" style="width: 100px">
                        Country
                    </th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
                </td>
                <td>
                    <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' />
                </td>
                <td>
                    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    

    并在代码后面使用此代码意味着 aspx.cs 页面

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindRepeater();
        }
    }
    
    private void BindRepeater()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Customers", con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    rptCustomers.DataSource = dt;
                    rptCustomers.DataBind();
                }
            }
        }
    }
    

    【讨论】:

    • 我使用的是 Visual Studio .net 2003。'configurationManager' 在这个版本中不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-12-10
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2012-02-24
    相关资源
    最近更新 更多