By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,505 Members | 1,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,505 IT Pros & Developers. It's quick & easy.

Need Help: Filling HTML table with data in C#

P: n/a
Hi All, New to the whole .Net and C# thing but trying.
In classic asp this was simple to fill a table with dynamic content and
hyperlinks.
Here is an example of what I am trying to do in classic asp:

<table width="640" border="0" cellspacing="3" cellpadding="3">
<tr class="mcsmalltextbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

<!-- #Include file="ADOVBS.INC" -->
<%
dim db, rs, sql, strFname, strLname, strPhone, strEmail

set db = Server.CreateObject("ADODB.Connection")
dbConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" _
&server.MapPath("\contacts.mdb")";"
db.open dbConnection

set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOnly, adLockOptimistic

if rs.EOF then
response.write("<p class='errorClass'>There are currently no entries
in the database.</p>")
else
Do until rs.EOF
strFname = rs("fname")
strLname = rs("lname")
strPhone = rs("phone")
strEmail = rs("email")

response.write("<tr class='tableClass'><td>" & strLname & "</td>")
response.write("<td>" & strFname & "</td>")
response.write("<td>" & strPhone & "</td>")
response.write("<td><a href='mailto:" & strEmail & "'>" & strEmail &
"</a></td>")

rs.movenext
loop
end if

rs.close
set rs = nothing
db.close
set db = nothing
%>

</tr>
</table>

So how would I start in ASP.Net using C# ?
I need to do it this way so I get more control over the design.
Unless I am missing something.

Jan 19 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Look into using a Repeater. You have complete control over all the templates
to get the output you want, and you can use databinding, so that with one
line of code you bind all the data, and asp.net uses the templates to create
the appropriate output.

"Bill" <bi*************@yahoo.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
Hi All, New to the whole .Net and C# thing but trying.
In classic asp this was simple to fill a table with dynamic content and
hyperlinks.
Here is an example of what I am trying to do in classic asp:

<table width="640" border="0" cellspacing="3" cellpadding="3">
<tr class="mcsmalltextbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

<!-- #Include file="ADOVBS.INC" -->
<%
dim db, rs, sql, strFname, strLname, strPhone, strEmail

set db = Server.CreateObject("ADODB.Connection")
dbConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" _
&server.MapPath("\contacts.mdb")";"
db.open dbConnection

set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOnly, adLockOptimistic

if rs.EOF then
response.write("<p class='errorClass'>There are currently no entries
in the database.</p>")
else
Do until rs.EOF
strFname = rs("fname")
strLname = rs("lname")
strPhone = rs("phone")
strEmail = rs("email")

response.write("<tr class='tableClass'><td>" & strLname & "</td>")
response.write("<td>" & strFname & "</td>")
response.write("<td>" & strPhone & "</td>")
response.write("<td><a href='mailto:" & strEmail & "'>" & strEmail &
"</a></td>")

rs.movenext
loop
end if

rs.close
set rs = nothing
db.close
set db = nothing
%>

</tr>
</table>

So how would I start in ASP.Net using C# ?
I need to do it this way so I get more control over the design.
Unless I am missing something.

Jan 19 '07 #2

P: n/a
look at the gridview.

when converting from asp to asp.net there are two new rules for inline code.

1) function (methods) can only be defined in script block not with the
<% %syntax.

2) inline code can only be in <% %syntax and is not allowed in script
blocks.

also there is no include feature
-- bruce (sqlwork.com)

Bill wrote:
Hi All, New to the whole .Net and C# thing but trying.
In classic asp this was simple to fill a table with dynamic content and
hyperlinks.
Here is an example of what I am trying to do in classic asp:

<table width="640" border="0" cellspacing="3" cellpadding="3">
<tr class="mcsmalltextbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

<!-- #Include file="ADOVBS.INC" -->
<%
dim db, rs, sql, strFname, strLname, strPhone, strEmail

set db = Server.CreateObject("ADODB.Connection")
dbConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" _
&server.MapPath("\contacts.mdb")";"
db.open dbConnection

set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOnly, adLockOptimistic

if rs.EOF then
response.write("<p class='errorClass'>There are currently no entries
in the database.</p>")
else
Do until rs.EOF
strFname = rs("fname")
strLname = rs("lname")
strPhone = rs("phone")
strEmail = rs("email")

response.write("<tr class='tableClass'><td>" & strLname & "</td>")
response.write("<td>" & strFname & "</td>")
response.write("<td>" & strPhone & "</td>")
response.write("<td><a href='mailto:" & strEmail & "'>" & strEmail &
"</a></td>")

rs.movenext
loop
end if

rs.close
set rs = nothing
db.close
set db = nothing
%>

</tr>
</table>

So how would I start in ASP.Net using C# ?
I need to do it this way so I get more control over the design.
Unless I am missing something.
Jan 19 '07 #3

P: n/a
Another confusing issue for people moving from ASP to ASP.NET is
separating the code from the content.

I would suggest using the code behind file page.aspx.cs to place your
code in, and the goal should be to not having any HTML in this file.
All of the HTML should be on the page.aspx file. For this example you
would place the repeater in the page.asx file and your HTML would be
included in the various templates. THe HeaderTemplate would have the
<tabletag, the ItemTemplate would have the <tr></tr>, and the
FooterTemplate would have the </table>.

This way you are not doing string concatenation or Response.Write of
HTML in the code behind file.

Hope that helps,

Jeff

Jan 19 '07 #4

P: n/a
Asp.Net is object-oriented as opposed to Asp that is procedure-oriented.
This means in asp.net you choose your set of objects and populate them. In
your case, as the others said, you should use a gridview or repeater with an
ItemTemplate matching your row and databind it to your database table.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Bill" <bi*************@yahoo.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
Hi All, New to the whole .Net and C# thing but trying.
In classic asp this was simple to fill a table with dynamic content and
hyperlinks.
Here is an example of what I am trying to do in classic asp:

<table width="640" border="0" cellspacing="3" cellpadding="3">
<tr class="mcsmalltextbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

<!-- #Include file="ADOVBS.INC" -->
<%
dim db, rs, sql, strFname, strLname, strPhone, strEmail

set db = Server.CreateObject("ADODB.Connection")
dbConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" _
&server.MapPath("\contacts.mdb")";"
db.open dbConnection

set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOnly, adLockOptimistic

if rs.EOF then
response.write("<p class='errorClass'>There are currently no entries
in the database.</p>")
else
Do until rs.EOF
strFname = rs("fname")
strLname = rs("lname")
strPhone = rs("phone")
strEmail = rs("email")

response.write("<tr class='tableClass'><td>" & strLname & "</td>")
response.write("<td>" & strFname & "</td>")
response.write("<td>" & strPhone & "</td>")
response.write("<td><a href='mailto:" & strEmail & "'>" & strEmail &
"</a></td>")

rs.movenext
loop
end if

rs.close
set rs = nothing
db.close
set db = nothing
%>

</tr>
</table>

So how would I start in ASP.Net using C# ?
I need to do it this way so I get more control over the design.
Unless I am missing something.

Jan 21 '07 #5

P: n/a
Thanks for the great responses. I am familiar with all the methods
mentioned. The one hang up I am having from my original post is this.
Say I want to pull an email address from my database and wrap it in a
hyperlink like this... "<a href='mailto:" & strEmail & "'>" & strEmail
& "</a>" How do I pull that off in a repeater or gridview? Am using VS
2005, don't recall if I mentioned that in the original post.

Thanks gain,
Bill

Jan 22 '07 #6

P: n/a
i'm running into the same trouble. i built an aspx page with
<asp:tableand <asp:repeatertags. the repeater tag (and table) is
databound in the code-behind file. unfortunately, it must not be
binding the data correctly to the table cell because each cell is
getting mutiple records and not one record. here is my code.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>MyTable</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="tbl_Default" runat="server" />
<asp:Repeater ID="rpt_Default" runat="server" />
</div>
</form>
</body>
</html>
++++++++++++++++++++++++++++++codebehind++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++
public class MyDefault : Page
{
protected Table tbl_Default;
protected Repeater rpt_Default;

public SqlConnection conn;
public SqlCommand comm;
public SqlDataReader reader;

void Page_Load(object s, EventArgs e)
{
conn = new
SqlConnection("server=xxxx;uid=mp3Admin;pwd=xxxxxx x;database=xxx");
conn.Open();
comm = new SqlCommand("select * from _XXXX where ID<25", conn);
reader = comm.ExecuteReader();

rpt_Default.HeaderTemplate = new
MyTemplate(ListItemType.Header, tbl_Default);
rpt_Default.ItemTemplate = new MyTemplate(ListItemType.Item,
tbl_Default);
rpt_Default.AlternatingItemTemplate = new
MyTemplate(ListItemType.AlternatingItem, tbl_Default);
rpt_Default.DataSource = reader;
rpt_Default.DataBind();
}

}

// C#
public class MyTemplate : ITemplate
{
static int itemcount = 0;
ListItemType templateType;
Table myTable;
public MyTemplate(ListItemType type)
{
templateType = type;
}

public MyTemplate(ListItemType type, Table tbl)
{
templateType = type;
myTable = tbl;
}

public void InstantiateIn(System.Web.UI.Control container)
{
TableRow thr = new TableRow();
TableCell thc = new TableCell();
TableRow trI = new TableRow();
TableCell tcI = new TableCell();
switch (templateType)
{
case ListItemType.Header:
myTable.BackColor = Color.Orange;
thc.Text = "Items";
thr.Cells.Add(thc);
myTable.Rows.Add(thr);
break;
case ListItemType.Item:
tcI.Text = "Item Number : " + itemcount.ToString()
tcI.DataBinding += new
EventHandler(TemplateControlTC_DataBinding);
trI.Cells.Add(tcI);
break;
case ListItemType.AlternatingItem:
tcI.DataBinding += new
EventHandler(TemplateControlTC_DataBinding);
tcI.Text = "Item Number : " + itemcount.ToString()
trI.Cells.Add(tcI);
trI.BackColor = Color.Blue;
break;
case ListItemType.Footer:
break;
}
myTable.Rows.Add(trI);
container.Controls.Add(myTable);
itemcount += 1;
}

private void TemplateControlTC_DataBinding(object sender,
System.EventArgs e)
{
TableCell tc;
tc = (TableCell)sender;
RepeaterItem container = (RepeaterItem)tc.NamingContainer;
tc.Text += DataBinder.Eval(container.DataItem, "Filename");
}

if you take a look at the MS site under Creating Web Server Control
Templates Programmatically, the table is created via a label control's
text property.

http://msdn.microsoft.com/library/de...mmatically.asp

can someone show me how to get one record per table cell (like in the
MS example with the literal control).

thx

Jan 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.