ASP.NET中的HTML表格显示数据 [英] Show data in ASP.NET html table
问题描述
我写一个ASP.NET网页,其中从数据库中读取数据,需要显示它在表中。出于某种原因,我不想用GridView控件。
如何显示在HTML页面上表中的数据?
这是我的C#code:
SqlConnection的thisConnection =新的SqlConnection(的DbConnection);
的SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText =SELECT * FROM测试;
thisConnection.Open();
SqlDataReader的读者= thisCommand.ExecuteReader(); 而(reader.Read())
{
INT ID = reader.GetInt32(0);
字符串名称= reader.GetString(1);
字符串传递= reader.GetString(2);
} thisConnection.Close();
这是我的ASPX页面:
<%@页面语言=C#AutoEventWireup =真的MasterPageFile =〜/ test.mastercodeFILE =test.aspx.cs继承= 测试%GT;< ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =的ContentPlaceHolder>
<表格的宽度=100%ALIGN =中心的cellpadding =2CELLSPACING =2BORDER =0的bgcolor =#EAEAEA>
< TR align =left的风格=背景颜色:#004080;颜色:白色; >
&所述; TD> ID< / TD>
&所述; TD>名称和LT; / TD>
< TD>通< / TD>
< / TR> **<% - 需要的同时输出到这里 - %GT; ** < /表>
< / ASP:内容>
基本上使用传统的ASP \\ PHP \\意大利面code的方法。
首先,请将code。在返回字符串
一个私有方法。
该方法:
公共字符串getWhileLoopData()
{
字符串htmlStr =;
SqlConnection的thisConnection =新的SqlConnection(的DbConnection);
的SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText =SELECT * FROM测试;
thisConnection.Open();
SqlDataReader的读者= thisCommand.ExecuteReader(); 而(reader.Read())
{
INT ID = reader.GetInt32(0);
字符串名称= reader.GetString(1);
字符串传递= reader.GetString(2);
htmlStr +=\"<tr><td>\"+id+\"</td><td>\"+Name+\"</td><td>\"+Pass+\"</td></tr>\"
} thisConnection.Close();
返回htmlStr;
}
然后就可以使用&LT;%= getWhileLoopData()%&GT;
标记在ASP.NET等于&LT;%响应.WRITE(getWhileData())%GT;
它应该是这个样子:
&LT;%@页面语言=C#AutoEventWireup =真的MasterPageFile =〜/ test.mastercodeFILE =test.aspx.cs继承= 测试%GT;&LT; ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =的ContentPlaceHolder&GT;
&LT;表格的宽度=100%ALIGN =中心的cellpadding =2CELLSPACING =2BORDER =0的bgcolor =#EAEAEA&GT;
&LT; TR align =left的风格=背景颜色:#004080;颜色:白色; &GT;
&所述; TD&GT; ID&LT; / TD&GT;
&所述; TD&GT;名称和LT; / TD&GT;
&LT; TD&GT;通&LT; / TD&GT;
&LT; / TR&GT; &所述;%= getWhileLoopData()%&GT; &LT; /表&gt;
&LT; / ASP:内容&GT;
还有使用一个中继器控制,并从您的数据库中的数据绑定到你喜欢的项目模板选项。
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.
How do I show the data in a table on the HTML page?
This is my C# code:
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:
<%@ 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.
First of all, place your code in one private method that returns a 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())%>
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.
这篇关于ASP.NET中的HTML表格显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!