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

ASP.NET

Notes, Tricks and Tips on ASP.NET Coding
  • 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)

  • Disabling buttons on click in ASP.NET

    Just to give props, I found this here: http://geekswithblogs.net/jwhitehorn/archive/2006/06/17/82214.aspx

    but I don't like to leave valuable info on the internet, it might get deleted. Plus, I have to spend the 30 minutes hunting it down again <smiles>

    We have all seen the websites that disable "submit" buttons when you click on them. This is often done to prevent users from clicking the button multiple times.
    Normally this is accomplished using an 'onclick' JavaScript event to disable the button. In ASP.NET, each server side item already has a onclick event handler which calls the server back for event processing.
    To accomplish the same thing in ASP.NET, you could easily do:

     Button1.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(Button1, "").ToString()) 

  • Build a Table from Code

    Set the table on the aspx page to have a runat="server".

    <table id="tblReport" runat="server">
        <tr>
            <td width="100" class="ReportSubtitle">
                Unit
            </td>
            <td width="100" class="ReportSubtitle">
                Date
            </td>
            <td width="100" class="ReportSubtitle">
                           
            </td>
            <td width="100" class="ReportSubtitle">
                Cash Deposit
            </td>
            <td width="100" class="ReportSubtitle">
                Proof to Bank
            </td>
            <td width="100" class="ReportSubtitle">
                Initials
            </td>
        </tr>
    </table>

    In the code behind, add something like:

    Public Sub bindData()
            Dim ds As DataSet = CulinartDB.SPs.FpPOSDailySummaryReport(Session("startdate"), Session("enddate")).GetDataSet()
    
    
            'get a list of the units in the dataset
            Dim units = (From dr In ds.Tables(0).Rows _
                        Select locndscr = dr("locndscr"), locncode = dr("locncode"), total = dr("TotalCashDeposit")).Distinct()
    
    
            For Each u In units
                Dim locn = u.locncode
    
    
                'build the header
                Dim rowHeader As New HtmlTableRow()
                Dim cell As New HtmlTableCell()
                cell.ColSpan = 6
                cell.ID = "report_cell_title"
                cell.InnerHtml = u.locndscr
                rowHeader.Cells.Add(cell)
                tblReport.Rows.Add(rowHeader)
    
    
                'add the lines
                Dim lines = From dr In ds.Tables(0) _
                            Select docdate = dr("docdate"), cashDeposit = dr("CashDeposit"), totalCashDeposit = dr("TotalCashDeposit"), locncode = dr("locncode") _
                            Where locncode = locn
    
    
                For Each l In lines
                    Dim row As New HtmlTableRow()
                    Dim cName As New HtmlTableCell()
                    Dim cDt As New HtmlTableCell()
                    Dim cTotalDesc As New HtmlTableCell()
                    Dim cCashDeposit As New HtmlTableCell()
                    Dim cProof As New HtmlTableCell()
                    Dim cInit As New HtmlTableCell()
    
    
                    'set the properties on the cells
                    cName.ID = "report_cell"
                    cDt.ID = "report_cell"
                    cTotalDesc.ID = "report_cell"
                    cCashDeposit.ID = "report_cell"
                    cProof.ID = "report_cell_blank"
                    cInit.ID = "report_cell_blank"
    
    
                    'set the cell text
                    cDt.InnerText = Date.Parse(l.docdate).ToShortDateString()
                    cTotalDesc.InnerText = "Daily Total"
                    cCashDeposit.InnerText = CType(l.cashDeposit, Double).ToString("C")
                    cProof.InnerHtml = "&nbsp;"
                    cInit.InnerHtml = "&nbsp;"
    
    
                    'add the cells to the row
                    row.Cells.Add(cName)
                    row.Cells.Add(cDt)
                    row.Cells.Add(cTotalDesc)
                    row.Cells.Add(cCashDeposit)
                    row.Cells.Add(cProof)
                    row.Cells.Add(cInit)
    
    
                    'add the row to the table
                    tblReport.Rows.Add(row)
    
    
    
                Next
    
    
                'build the footer
                Dim rowFooter As New HtmlTableRow()
                Dim cNameF As New HtmlTableCell()
                Dim cDtF As New HtmlTableCell()
                Dim cTotalDescF As New HtmlTableCell()
                Dim cCashDepositF As New HtmlTableCell()
                Dim cProofF As New HtmlTableCell()
                Dim cInitF As New HtmlTableCell()
    
    
                'set the properties
                cNameF.ID = "report_cell"
                cDtF.ID = "report_cell"
                cTotalDescF.ID = "report_cell_title"
                cCashDepositF.ID = "report_cell_title"
                cProofF.ID = "report_cell_blank"
                cInitF.ID = "report_cell_blank"
    
    
                'set the text
                cTotalDescF.InnerText = "Unit Total"
                cCashDepositF.InnerText = CType(u.total, Double).ToString("C")
    
    
                'add the cells to the row
                rowFooter.Cells.Add(cNameF)
                rowFooter.Cells.Add(cDtF)
                rowFooter.Cells.Add(cTotalDescF)
                rowFooter.Cells.Add(cCashDepositF)
                rowFooter.Cells.Add(cProofF)
                rowFooter.Cells.Add(cInitF)
    
    
                'add the row
                tblReport.Rows.Add(rowFooter)
    
    
            Next
    
    
    
        End Sub

More Posts Next page »