Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Need Help: Filling HTML table with data in C#

Reply
Thread Tools

Need Help: Filling HTML table with data in C#

 
 
Bill
Guest
Posts: n/a
 
      01-19-2007
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.

 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      01-19-2007
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.
>

 
Reply With Quote
 
 
 
 
Marina Levit [MVP]
Guest
Posts: n/a
 
      01-19-2007
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" <> wrote in message
news: 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.
>



 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      01-19-2007
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
<table> tag, 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

 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      01-21-2007
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" <> wrote in message
news: 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.
>



 
Reply With Quote
 
Bill
Guest
Posts: n/a
 
      01-22-2007
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

 
Reply With Quote
 
Mr Not So Know It All
Guest
Posts: n/a
 
      01-24-2007
i'm running into the same trouble. i built an aspx page with
<asp:table> and <asp:repeater> tags. 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Image not filling a table table (html4 strict) killy971 HTML 21 08-15-2008 09:58 PM
how to generate html table from "table" data? petr.jakes.tpc@gmail.com Python 9 12-28-2007 09:44 AM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
Filling table of cell with <input> fields Shef HTML 4 12-10-2003 03:51 AM
Changing a html table when new data is entered into SQL table. Renie83 ASP General 1 07-09-2003 03:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57