VB.net Dynamic asp:从DataTable复制行创建的表 - 需要建议

时间:2014-02-18 13:42:08

标签: asp.net sql vb.net

我的asp表单中有一个动态创建的表的问题。

要控制页面并创建表格,用户选择日期范围并单击“刷新”按钮。

该表在后面的代码中填充后创建,但重复, 即应填充11行,但在第11行之后,第12-22行与1 - 11重复。

有趣的是,当点击一个按钮(由动态后面的代码创建)时,页面会回发,表格应该是第1 - 11行,而不是更多。我在我的Page_Load sub。中复制了下面的代码。

请提前告知,谢谢;

Protected Sub Refresh_Click(sender As Object, e As EventArgs)

    '/*** CREATES CONNECTION STRING AND OPENS CONNECTION TO DB ***\'
    Dim cn As New OleDbConnection

    cn.ConnectionString = [String]
    cn.Open()

    '/*** CREATES DateFormat FOR PLACEMENT WITHIN THE SQL ***\'
    Dim dtform As String
    dtform = "dd-MMM-yyyy"

    '/*** DECLARES AND FINDS FROM & TO DATE FOR SQL STATEMENT ***\'
    Dim from As Date
    from = Frmdt.Text

    Dim todat As Date
    todat = Todt.Text

    '/*** EXECUTES SQL & CREATES RECORDSET WITH RecordCount ***\'
    Dim rs As New ADODB.Recordset()
    Dim x As Integer

    With rs
        .CursorLocation = ADODB.CursorLocationEnum.adUseClient
        .CursorType = ADODB.CursorTypeEnum.adOpenStatic
        .LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
        .Open("SELECT * FROM DB", cn.ConnectionString)
        x = rs.RecordCount
    End With

    '/*** DECLARES & FILLS DataAdapter, DataSet & DataTable ***\'
    Dim da As New OleDbDataAdapter()

    Dim ds As New DataSet()
    da.Fill(ds, rs, "[Field Headers]")

    Dim dt As DataTable = ds.Tables("[Field Headers]")

    '/*** DECLARES ROW COUNTER TO TRACK NUMBER OF ROWS ADDED TO ASP:TABLE ***\'
    Dim rwcntr As Integer

    '/*** DECLARES ROW COUNTER TO EXTRACT DATA FROM DataTable ***\'
    Dim i As Integer

    rwcntr = 1

    '/*** BEGINS LOOP OF DataTable & CREATES ASP:TABLE, ENDING WHEN ROW COUNTER REACHES RecordCount MINUS 1 ***\'
    For i = 0 To x - 1 Step +1

        Dim td As New TableCell()
        Dim td2 As New TableCell()
        Dim td3 As New TableCell()
        Dim td4 As New TableCell()
        Dim td5 As New TableCell()
        Dim td6 As New TableCell()
        Dim td7 As New TableCell()
        Dim td8 As New TableCell()
        Dim td9 As New TableCell()
        Dim td10 As New TableCell()
        Dim td11 As New TableCell()
        Dim td12 As New TableCell()
        Dim td13 As New TableCell()

        Dim tr As New TableRow()

        td.Text = dt.Rows(i)("[Field Header1]").ToString()
        td.Font.Name = "Calibri"
        td.BackColor = ColorTranslator.FromHtml("#cccccc")
        td.ForeColor = Color.Black
        td.Font.Size = 8
        td.BorderStyle = WebControls.BorderStyle.Inset
        td.BorderColor = Color.Black
        td.BorderWidth = 1.5
        td.EnableViewState = True
        td.HorizontalAlign = WebControls.HorizontalAlign.Center
        td.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td)

        td2.Text = dt.Rows(i)("[Field Header2]").ToString()
        td2.Font.Name = "Calibri"
        td2.BackColor = ColorTranslator.FromHtml("#cccccc")
        td2.ForeColor = Color.Black
        td.BorderWidth = 1.5
        td2.Font.Size = 8
        td2.BorderStyle = WebControls.BorderStyle.Inset
        td2.BorderColor = Color.Black
        td2.BorderWidth = 1.5
        td2.EnableViewState = True
        td2.HorizontalAlign = WebControls.HorizontalAlign.Center
        td2.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td2)

        td3.Text = dt.Rows(i)("[Field Header3]").ToString()
        td3.Font.Name = "Calibri"
        td3.BackColor = ColorTranslator.FromHtml("#cccccc")
        td3.ForeColor = Color.Black
        td3.BorderWidth = 1.5
        td3.Font.Size = 8
        td3.BorderStyle = WebControls.BorderStyle.Inset
        td3.BorderColor = Color.Black
        td.BorderWidth = 1.5
        td3.EnableViewState = True
        td3.HorizontalAlign = WebControls.HorizontalAlign.Center
        td3.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td3)

        td4.Text = dt.Rows(i)("[Field Header4]").ToString()
        td4.Font.Name = "Calibri"
        td4.BackColor = ColorTranslator.FromHtml("#cccccc")
        td4.ForeColor = Color.Black
        td4.Font.Size = 8
        td4.BorderStyle = WebControls.BorderStyle.Inset
        td4.BorderColor = Color.Black
        td4.BorderWidth = 1.5
        td4.EnableViewState = True
        td4.HorizontalAlign = WebControls.HorizontalAlign.Center
        td4.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td4)

        td5.Text = dt.Rows(i)("[Field Header5]").ToString()
        td5.Font.Name = "Calibri"
        td5.BackColor = ColorTranslator.FromHtml("#cccccc")
        td5.ForeColor = Color.Black
        td5.Font.Size = 8
        td5.BorderStyle = WebControls.BorderStyle.Inset
        td5.BorderColor = Color.Black
        td5.BorderWidth = 1.5
        td5.EnableViewState = True
        td5.HorizontalAlign = WebControls.HorizontalAlign.Center
        td5.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td5)

        td6.Text = dt.Rows(i)("[Field Header6]").ToString()
        td6.Font.Name = "Calibri"
        td6.BackColor = ColorTranslator.FromHtml("#cccccc")
        td6.ForeColor = Color.Black
        td6.Font.Size = 8
        td6.BorderStyle = WebControls.BorderStyle.Inset
        td6.BorderColor = Color.Black
        td6.BorderWidth = 1.5
        td6.EnableViewState = True
        td6.HorizontalAlign = WebControls.HorizontalAlign.Center
        td6.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td6)

        td7.Text = dt.Rows(i)("[Field Header7]").ToString()
        td7.Font.Name = "Calibri"
        td7.BackColor = ColorTranslator.FromHtml("#cccccc")
        td7.ForeColor = Color.Black
        td7.Font.Size = 8
        td7.BorderStyle = WebControls.BorderStyle.Inset
        td7.BorderColor = Color.Black
        td7.BorderWidth = 1.5
        td7.EnableViewState = True
        td7.HorizontalAlign = WebControls.HorizontalAlign.Center
        td7.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td7)

        td8.Text = dt.Rows(i)("[Field Header8]").ToString()
        td8.Font.Name = "Calibri"
        td8.BackColor = ColorTranslator.FromHtml("#cccccc")
        td8.ForeColor = Color.Black
        td8.Font.Size = 8
        td8.BorderStyle = WebControls.BorderStyle.Inset
        td8.BorderColor = Color.Black
        td8.BorderWidth = 1.5
        td8.EnableViewState = True
        td8.HorizontalAlign = WebControls.HorizontalAlign.Center
        td8.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td8)

        td9.Text = dt.Rows(i)("[Field Header9]").ToString()
        td9.Font.Name = "Calibri"
        td9.BackColor = ColorTranslator.FromHtml("#cccccc")
        td9.ForeColor = Color.Black
        td9.Font.Size = 8
        td9.BorderStyle = WebControls.BorderStyle.Inset
        td9.BorderColor = Color.Black
        td9.BorderWidth = 1.5
        td9.Width = 25
        td9.EnableViewState = True
        td9.HorizontalAlign = WebControls.HorizontalAlign.Center
        td9.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td9)

        'td10.Text = dt.Rows(i)("[Field Header10]").ToString()
        'td10.Font.Name = "Calibri"
        td10.BackColor = ColorTranslator.FromHtml("#cccccc")
        td10.ForeColor = Color.Black
        'td10.Font.Size = 8
        td10.BorderStyle = WebControls.BorderStyle.Inset
        td10.BorderColor = Color.Black
        td10.BorderWidth = 1.5
        td10.EnableViewState = True

        '/*** SHOULD DATA BE AVAILABLE CREATES ASP:BUTTON TO COPY TO CLIPBOARD LATER ***\'
        If Len(dt.Rows(i)("[Field Header10]").ToString()) > 0 Then

            Dim btn As New Button
            td10.Controls.Add(btn)
            btn.Text = dt.Rows(i)("[Field Header10]").ToString()
            btn.Font.Name = "Calibri"
            btn.Font.Size = 8
            td10.HorizontalAlign = WebControls.HorizontalAlign.Center
            td10.VerticalAlign = VerticalAlign.Middle
            btn.EnableViewState = True
            AddHandler btn.Click, AddressOf OnDbleClick

        End If
        tr.Controls.Add(td10)

        td11.Text = dt.Rows(i)("[Field Header11]").ToString()
        td11.Font.Name = "Calibri"
        td11.BackColor = ColorTranslator.FromHtml("#cccccc")
        td11.ForeColor = Color.Black
        td11.Font.Size = 8
        td11.BorderStyle = WebControls.BorderStyle.Inset
        td11.BorderColor = Color.Black
        td11.BorderWidth = 1.5
        td11.EnableViewState = True
        td11.HorizontalAlign = WebControls.HorizontalAlign.Center
        td11.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td11)

        td12.Text = dt.Rows(i)("[Field Header12]").ToString()
        td12.Font.Name = "Calibri"
        td12.BackColor = ColorTranslator.FromHtml("#cccccc")
        td12.ForeColor = Color.Black
        td12.Font.Size = 8
        td12.BorderStyle = WebControls.BorderStyle.Inset
        td12.BorderColor = Color.Black
        td12.BorderWidth = 1.5
        td12.EnableViewState = True
        td12.HorizontalAlign = WebControls.HorizontalAlign.Center
        td12.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td12)

        td13.Text = dt.Rows(i)("[Field Header13]").ToString()
        td13.Font.Name = "Calibri"
        td13.BackColor = ColorTranslator.FromHtml("#cccccc")
        td13.ForeColor = Color.Black
        td13.Font.Size = 8
        td13.BorderStyle = WebControls.BorderStyle.Inset
        td13.BorderColor = Color.Black
        td13.BorderWidth = 1.5
        td13.EnableViewState = True
        td13.HorizontalAlign = WebControls.HorizontalAlign.Center
        td13.VerticalAlign = VerticalAlign.Middle
        tr.Controls.Add(td13)


        '/*** ADDS ROW ONCE CELLS ARE COMPLETE ***\'
        itemstable.Controls.AddAt(rwcntr, tr)
        rwcntr = rwcntr + 1

    Next

end sub

0 个答案:

没有答案