Need Help: Filling HTML table with data in C#

Discussion in 'ASP .Net' started by Bill, Jan 19, 2007.

  1. Bill

    Bill Guest

    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.
     
    Bill, Jan 19, 2007
    #1
    1. Advertising

  2. Bill

    bruce barker Guest

    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.
    >
     
    bruce barker, Jan 19, 2007
    #2
    1. Advertising

  3. 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:...
    > 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.
    >
     
    Marina Levit [MVP], Jan 19, 2007
    #3
  4. Bill

    Jeff Guest

    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
     
    Jeff, Jan 19, 2007
    #4
  5. 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:...
    > 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.
    >
     
    Eliyahu Goldin, Jan 21, 2007
    #5
  6. Bill

    Bill Guest

    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
     
    Bill, Jan 22, 2007
    #6
  7. 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=xxxxxxx;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/d...webservercontroltemplatesprogrammatically.asp

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

    thx
     
    Mr Not So Know It All, Jan 24, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shef
    Replies:
    4
    Views:
    1,320
    Leif K-Brooks
    Dec 10, 2003
  2. Rio
    Replies:
    4
    Views:
    1,241
  3. killy971
    Replies:
    21
    Views:
    1,818
    dorayme
    Aug 15, 2008
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    146
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    133
Loading...

Share This Page