C# 在 ASP.NET html 表中显示数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13860490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:59:45  来源:igfitidea点击:

Show data in ASP.NET html table

c#asp.nethtml

提问by riad

I am writing an ASP.NET page which reads data from a database and needs to show it in a table. For some reason, I don't want to use the gridView.

我正在编写一个 ASP.NET 页面,该页面从数据库中读取数据并需要将其显示在表中。出于某种原因,我不想使用 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>

采纳答案by Mortalus

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. The method:

首先,将您的代码放在一个返回string. 方法:

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())%>

然后你可以<%=getWhileLoopData()%>在 ASP.NET 中使用等于的标签<%Response.Write(getWhileData())%>

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.

还可以选择使用转发器控件并将数据库中的数据绑定到您喜欢的项目模板。

回答by Mahdi Tahsildari

add 3 literalcontrols to your asp page put them in the trbelow the title tds, and then:

将 3 个literal控件添加到您的 asp 页面,将它们放在tr标题下方td,然后:

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 htmlcode:

这是我知道的最简单的解决方案,但它并不整洁。
这是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>

回答by Captain Kenpachi

If you put the runat="server"attribute on your HTML table, you can actually programmatically access it in your code-behind. It's quite a powerful feature.

如果将runat="server"属性放在 HTML 表中,则实际上可以在代码隐藏中以编程方式访问它。这是一个非常强大的功能。

It's definitely not the best solution, but it's a very cool feature no one knows about.

这绝对不是最好的解决方案,但这是一个没人知道的非常酷的功能。

回答by Manibhadra

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

谢谢

回答by Saeed Dini

In this solution , all HTML tags create in client Side and client have to render this processes instead of server. this is a better idea and give best performance for your server then use this code:

在这个解决方案中,所有在客户端和客户端创建的 HTML 标签都必须呈现这个过程而不是服务器。这是一个更好的主意,可以为您的服务器提供最佳性能,然后使用以下代码:

<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>