Site Search:
Sign in | Join | Help
4Penny.net

ASP.NET

Notes, Tricks and Tips on ASP.NET Coding

August 2008 - Posts

  • Adding lines to an HTML Table in code (VB.NET)

    Here's a useful tip for adding lines to an html table in code. Normally I'd define and populate a grid, but sometimes that's overkill 

     

     'declare the row
    Dim tr As New HtmlTableRow
    
    
    'declare the new cells
    Dim td1 As New HtmlTableCell
    Dim td2 As New HtmlTableCell
    
    
    'set the content
    td1.InnerHtml = "something"
    td2.InnerHtml = "something else"
    
    
    'set the ID property. This allows you to attach a CSS class to the cell
    td1.ID = "report_cell"
    td2.ID = "report_cell_title"
    
    
    'another way to set a style
    td1.Style.Add("width", "50px")
    
    
    'add the cells to the row
    tr.Cells.Add(td1)
    tr.Cells.Add(td2)
    
    
    'add the row to the table. This table is not declared in the code behind, it is a reference to a table in the 
    '.aspx page declared like this:
    '<table id="tblReport" runat="server"></table>
    tblReport.Rows.Add(tr)