473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help: Filling HTML table with data in C#

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="mcsmallt extbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

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

set db = Server.CreateOb ject("ADODB.Con nection")
dbConnection = "Driver={Micros oft Access Driver (*.mdb)};DBQ=" _
&server.MapPath ("\contacts.mdb ")";"
db.open dbConnection

set rs = Server.CreateOb ject("ADODB.Rec ordset")
rs.CursorLocati on = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOn ly, adLockOptimisti c

if rs.EOF then
response.write( "<p class='errorCla ss'>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='tableCla ss'><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
6 6520
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.comwrot e in message
news:11******** **************@ 11g2000cwr.goog legroups.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="mcsmallt extbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

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

set db = Server.CreateOb ject("ADODB.Con nection")
dbConnection = "Driver={Micros oft Access Driver (*.mdb)};DBQ=" _
&server.MapPath ("\contacts.mdb ")";"
db.open dbConnection

set rs = Server.CreateOb ject("ADODB.Rec ordset")
rs.CursorLocati on = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOn ly, adLockOptimisti c

if rs.EOF then
response.write( "<p class='errorCla ss'>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='tableCla ss'><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
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="mcsmallt extbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

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

set db = Server.CreateOb ject("ADODB.Con nection")
dbConnection = "Driver={Micros oft Access Driver (*.mdb)};DBQ=" _
&server.MapPath ("\contacts.mdb ")";"
db.open dbConnection

set rs = Server.CreateOb ject("ADODB.Rec ordset")
rs.CursorLocati on = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOn ly, adLockOptimisti c

if rs.EOF then
response.write( "<p class='errorCla ss'>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='tableCla ss'><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
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
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.comwrot e in message
news:11******** **************@ 11g2000cwr.goog legroups.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="mcsmallt extbold">
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
<td>Email Address</td>
</tr>

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

set db = Server.CreateOb ject("ADODB.Con nection")
dbConnection = "Driver={Micros oft Access Driver (*.mdb)};DBQ=" _
&server.MapPath ("\contacts.mdb ")";"
db.open dbConnection

set rs = Server.CreateOb ject("ADODB.Rec ordset")
rs.CursorLocati on = 3
rs.CursorType = 2
rs.LockType = 2
sql = "SELECT fname, lname, phone, email FROM tblAddressBook ORDER BY
lname;"
rs.Open sql, db, adOpenForwardOn ly, adLockOptimisti c

if rs.EOF then
response.write( "<p class='errorCla ss'>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='tableCla ss'><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
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
i'm running into the same trouble. i built an aspx page with
<asp:tableand <asp:repeaterta gs. 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>MyTabl e</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(objec t s, EventArgs e)
{
conn = new
SqlConnection(" server=xxxx;uid =mp3Admin;pwd=x xxxxxx;database =xxx");
conn.Open();
comm = new SqlCommand("sel ect * from _XXXX where ID<25", conn);
reader = comm.ExecuteRea der();

rpt_Default.Hea derTemplate = new
MyTemplate(List ItemType.Header , tbl_Default);
rpt_Default.Ite mTemplate = new MyTemplate(List ItemType.Item,
tbl_Default);
rpt_Default.Alt ernatingItemTem plate = new
MyTemplate(List ItemType.Altern atingItem, tbl_Default);
rpt_Default.Dat aSource = reader;
rpt_Default.Dat aBind();
}

}

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

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

public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
TableRow thr = new TableRow();
TableCell thc = new TableCell();
TableRow trI = new TableRow();
TableCell tcI = new TableCell();
switch (templateType)
{
case ListItemType.He ader:
myTable.BackCol or = Color.Orange;
thc.Text = "Items";
thr.Cells.Add(t hc);
myTable.Rows.Ad d(thr);
break;
case ListItemType.It em:
tcI.Text = "Item Number : " + itemcount.ToStr ing()
tcI.DataBinding += new
EventHandler(Te mplateControlTC _DataBinding);
trI.Cells.Add(t cI);
break;
case ListItemType.Al ternatingItem:
tcI.DataBinding += new
EventHandler(Te mplateControlTC _DataBinding);
tcI.Text = "Item Number : " + itemcount.ToStr ing()
trI.Cells.Add(t cI);
trI.BackColor = Color.Blue;
break;
case ListItemType.Fo oter:
break;
}
myTable.Rows.Ad d(trI);
container.Contr ols.Add(myTable );
itemcount += 1;
}

private void TemplateControl TC_DataBinding( object sender,
System.EventArg s e)
{
TableCell tc;
tc = (TableCell)send er;
RepeaterItem container = (RepeaterItem)t c.NamingContain er;
tc.Text += DataBinder.Eval (container.Data Item, "Filename") ;
}

if you take a look at the MS site under Creating Web Server Control
Templates Programmaticall y, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
15026
by: Sugapablo | last post by:
Before I go building this, I want to know if it already exists. I need some PHP code that will read a web page and return all text that comes between <td></td> tags in an array. So if there were three tables on that page, it would return the first table's fourth row, third column in a variable such as: $tableArray // ^ ^ ^ -...
2
1355
by: serge | last post by:
Using some VB sample code on the Internet I have the following that works well and exports a set of records to an XML file: Dim oCmd, sSQL, oDom Set oDom = CreateObject("Msxml2.DOMDocument.4.0") Set oCmd = CreateObject("ADODB.Command") oCmd.ActiveConnection = "Provider=SQLOLEDB;Data Source=(local);Initial...
0
1068
by: gg77 | last post by:
Hi, I don't have much knowledge of the table control. I am basically trying to use a table thats populated with data rows from a dataSet but currently things aren't working for me. below is the sample HTML that I have: <asp:TableRow> <asp:TableCell Text="Company" ID="CompanyCode"
1
1136
by: Danny | last post by:
I am using the webcontrol in access to download links and it is nice. But do you know if there is a way to extract specific tags? I need to extract data from a table in a web page and into a database. It is a table with 5 rows. Thanks in advance. Thanks in advance
3
1498
by: gg77 | last post by:
Hi, I don't have much knowledge of the table control. I am basically trying to use a table thats populated with data rows from a dataSet but currently things aren't working for me. below is the sample HTML that I have: <asp:TableRow> <asp:TableCell Text="Company" ID="CompanyCode"
2
1683
by: rick | last post by:
Greetings, I am trying to generate an html table that looks through the following xml source and lists links to all of the files (resource-file) and finds the resource-forms that match those file names and adds the appropriate file description next to the link to the filename. The xslt code that I've included doesn't work but gives an idea...
1
2941
crystal2005
by: crystal2005 | last post by:
Hi i encounterd some problem when i implement CSS for rounded Corner table. Need help please... :D <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content="Heaventure Sole-Proprietor"> <meta name="keywords" content="Heaventure, Company, Heaventure Company, Singapore"...
12
4324
by: ab12 | last post by:
I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape from which to start filling. I need to use recursion here. for example, to be clear: input: (ignore the line, think of that as blank space)...
2
2687
by: susinthaa | last post by:
Hi All, Good Morning! My requirement is as below: I am having a HTML table and the text boxes are embedded as a row of the tabel. And I am having a button at the end of the page. When I click the submit button, the datas entered in the HTML table should be inserted into the access database.
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.