Show data in ASP.NET html table

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

How to show data in a table on an 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> 
+6
source share
5 answers

Mostly the classic ASP \ PHP \ Spaghetti approach is used.

First of all, put your code in one public method that returns string . 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()%> in ASP.NET, which is <%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 the repeater control and bind the data from your database to the element template to your liking.

+13
source

I suggest you use the repeater control and create the html table structure in the repeater.

 <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 the repeater the same way you bind the gridview

thanks

+2
source

If you put the runat="server" attribute in an HTML table, you can programmatically access it in code. This is a pretty powerful feature.

This is definitely not the best solution, but it is a very cool feature that no one knows about.

+1
source

add 3 literal controls to your asp page, put them in tr under td s heading, and then:

 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 that I know, but it is not neat.
here's the html code:

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

In this solution, all HTML tags are created on the client side, and the client must display these processes instead of the server. This is the best idea and 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> 
0
source

All Articles