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

ASP.NET

Notes, Tricks and Tips on ASP.NET Coding

May 2008 - Posts

  • 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

  • Disabling a Button when Clicked

    To disable a button when it is clicked, add the following to the load event:

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

    This will cause the button to disable via javascript before the postback happens.  This prevents things like credit card transactions from going through more than once.