简体   繁体   English

在ASP.NET html表中显示数据

[英]Show data in ASP.NET html table

I am writing an ASP.NET page which reads data from a database and needs to show it in a table. 我正在编写一个ASP.NET页面,它从数据库中读取数据并需要在表中显示它。 For some reason, I don't want to use the gridView. 出于某种原因,我不想使用gridView。

How do I show the data in a table on the HTML page? 如何在HTML页面的表格中显示数据?

This is my C# code: 这是我的C#代码:

        SqlConnection thisConnection = new SqlConnection(dbConnection);
        SqlCommand thisCommand = thisConnection.CreateCommand();
        thisCommand.CommandText = "SELECT * from Test";
        thisConnection.Open();
        SqlDataReader reader = thisCommand.ExecuteReader();

        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string Name = reader.GetString(1);
            string Pass = reader.GetString(2);                   
        }

        thisConnection.Close();

This is my ASPX page: 这是我的ASPX页面:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/test.master" CodeFile="test.aspx.cs" Inherits="test" %>

<asp:Content ID="BodyContent" runat="server"  ContentPlaceHolderID="ContentPlaceHolder">
    <table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" bgcolor="#EAEAEA" >
        <tr align="left" style="background-color:#004080;color:White;" >
            <td> ID </td>                        
            <td> Name </td>            
            <td>Pass</td>                        
        </tr>

        **<%--Need the while output into here--%>**

    </table>
</asp:Content>

Basically use the classic ASP\\PHP\\Spaghetti code approach. 基本上使用经典的ASP \\ PHP \\ Spaghetti代码方法。

First of all, place your code in one public method that returns a string . 首先,将代码放在一个返回string公共方法中。 The method: 方法:

public string getWhileLoopData()
{
        string htmlStr = "";
        SqlConnection thisConnection = new SqlConnection(dbConnection);
        SqlCommand thisCommand = thisConnection.CreateCommand();
        thisCommand.CommandText = "SELECT * from Test";
        thisConnection.Open();
        SqlDataReader reader = thisCommand.ExecuteReader();

        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string Name = reader.GetString(1);
            string Pass = reader.GetString(2);
            htmlStr +="<tr><td>"+id+"</td><td>"+Name+"</td><td>"+Pass+"</td></tr>"                   
        }

        thisConnection.Close();
        return htmlStr;
}

Then you can use the <%=getWhileLoopData()%> tag in ASP.NET that is equal to <%Response.Write(getWhileData())%> 然后,您可以在ASP.NET中使用等于<%Response.Write(getWhileData())%><%=getWhileLoopData()%>标记

It should look something like this: 它应该看起来像这样:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/test.master" CodeFile="test.aspx.cs" Inherits="test" %>

<asp:Content ID="BodyContent" runat="server"  ContentPlaceHolderID="ContentPlaceHolder">
    <table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" bgcolor="#EAEAEA" >
        <tr align="left" style="background-color:#004080;color:White;" >
            <td> ID </td>                        
            <td> Name </td>            
            <td>Pass</td>                        
        </tr>

        <%=getWhileLoopData()%>

    </table>
</asp:Content>

There is also the option to use an repeater control and bind the data from your DB to an Item Template of your liking. 还可以选择使用转发器控件并将数据库中的数据绑定到您喜欢的项目模板。

I suggest you to use repeater control and create your html table structure in repeater. 我建议你使用转发器控件并在转发器中创建你的html表结构。

<table cellpadding="0" cellspacing="0" width="100%">
<asp:Repeater ID="rpt" runat="server" >
<HeaderTemplate>
<tr class="Header">
<td>
ID
</td>
<td>
Name
</td>
<td>
Pass
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("ID")%>
</td>
<td>
<%# Eval("Name")%>
</td>
<td>
<%# Eval("Pass")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

bind repeater as same as you bind gridview 绑定转发器与绑定gridview相同

thanks 谢谢

If you put the runat="server" attribute on your HTML table, you can actually programmatically access it in your code-behind. 如果在HTML表上放置runat="server"属性,您实际上可以在代码隐藏中以编程方式访问它。 It's quite a powerful feature. 这是一个非常强大的功能。

It's definitely not the best solution, but it's a very cool feature no one knows about. 它绝对不是最好的解决方案,但它是一个非常酷的功能,没有人知道。

add 3 literal controls to your asp page put them in the tr below the title td s, and then: 在你的asp页面中添加3个literal控件,将它们放在标题td下方的tr中,然后:

while (reader.Read())
    {
        int id = reader.GetInt32(0);
        string Name = reader.GetString(1);
        string Pass = reader.GetString(2);  
        literalID.text += id.ToString();
        literalName.text += Name.ToString();
        literalPass.text += Pass.ToString();
    }

this is the easiest solution I know, but it's not neat. 这是我所知道的最简单的解决方案,但它并不整洁。
here's the html code: 这是html代码:

<tr align="left" style="background-color:#004080;color:White;" >
    <td> ID </td>                        
    <td> Name </td>            
    <td>Pass</td>                        
</tr>
<tr align="left" style="background-color:#004080;color:White;" >
    <td><asp:Literal ID="literalID" runat="server"></asp:Literal></td>                        
    <td><asp:Literal ID="literalName" runat="server"></asp:Literal></td>            
    <td><asp:Literal ID="literalPass" runat="server"></asp:Literal></td>   
</tr>

In this solution , all HTML tags create in client Side and client have to render this processes instead of server. 在此解决方案中,所有HTML标记都在客户端创建,客户端必须呈现此进程而不是服务器。 this is a better idea and give best performance for your server then use this code: 这是一个更好的主意,并为您的服务器提供最佳性能,然后使用此代码:

<asp:Content ID="BodyContent" runat="server" 
 ContentPlaceHolderID="ContentPlaceHolder">
     <table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" bgcolor="#EAEAEA" >
         <tr align="left" style="background-color:#004080;color:White;" >
             <td> ID </td>                        
             <td> Name </td>            
            <td>Pass</td>                        
         </tr>

       <% if(dt != null && dt.rows.count>0){
              foreach(var item in dt.rows)
                 {%>
                   <tr>
                      <td><%=item["ID"]%></td>
                      <td><%=item["Name"]%></td>
                      <td><%=item["Pass"]%></td>
                   </tr>
                  <%}
                }%>

     </table> </asp:Content>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM