ASP.NET VB.NET通过类似命名的表单控件循环

时间:2012-01-05 22:52:45

标签: asp.net vb.net

好的,所以我最近得到了一些人的帮助,学习如何在VB.Net中循环使用类似名称的表单控件。我现在正致力于将整个应用程序转换为Web应用程序,并且我处于相同的位置。任何人都可以帮我弄清楚我在做错了什么。我知道这一定很简单,但它暗指我。我附加了在VB.Net中运行的代码:

Public Conn As ADODB.Connection
Public Rs As ADODB.Recordset
Public Sql As String

Public Sub ConnOpenClose()

    Conn = New ADODB.Connection
    Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databaseLocation & ""
    Conn.Open()
End Sub


ConnOpenClose()

Rs = New ADODB.Recordset
Sql = "SELECT * FROM INVOICES WHERE CurrentStatus<>'Checked-Out' ORDER BY INVOICES.CurrentStatusNum, INVOICES.OpenedDate,OpenedTime;"
SortCategory = ""
Rs.Open(Sql, Conn)
If Rs.EOF = False Then
    Rs.Move(moveIndex)
    For a = 1 To 20
        If Rs.EOF = True Then
            Me.Controls("lblStatus" & a).Text = ""
            Me.Controls("lblStatus" & a).Visible = False
            Me.Controls("lblInvoice" & a).Text = ""
            Me.Controls("lblInvoice" & a).Visible = False
            Me.Controls("lblSystemMakeModel" & a).Text = ""
            Me.Controls("lblSystemMakeModel" & a).Visible = False
            cmdScrollDown.Enabled = False
        Else
            If SortCategory <> Rs.Fields("CurrentStatus").Value Then
                SortCategory = Rs.Fields("CurrentStatus").Value
                tempInteger = a
                If Rs.Fields("CurrentStatus").Value = "Checked In" Then
                    Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblCheckedIn").Text
                ElseIf Rs.Fields("CurrentStatus").Value = "On Bench" Then
                    Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblOnBench").Text
                ElseIf Rs.Fields("CurrentStatus").Value = "Update" Then
                    Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblUpdate").Text
                ElseIf Rs.Fields("CurrentStatus").Value = "Contact Us" Then
                    Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblContactUs").Text
                ElseIf Rs.Fields("CurrentStatus").Value = "Finished" Then
                    Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblFinished").Text
                ElseIf Rs.Fields("CurrentStatus").Value = "To Be Scrapped" Then
                    Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblToBeScrapped").Text
                Else
                    Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value
                End If
                Me.Controls("lblStatus" & a).Visible = True
                Me.Controls("lblInvoice" & a).Text = ""
                Me.Controls("lblInvoice" & a).Visible = False
                Me.Controls("lblSystemMakeModel" & a).Text = ""
                Me.Controls("lblSystemMakeModel" & a).Visible = False
            Else
                Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value
                Me.Controls("lblStatus" & a).Visible = False
                Me.Controls("lblInvoice" & a).Text = Rs.Fields("InvoiceID").Value & " - " & Rs.Fields("CustomerFName").Value & " " & Rs.Fields("CustomerLName").Value & " (" & DateDiff("d", Rs.Fields("OpenedDate").Value, Now()) & " days)"
                Me.Controls("lblInvoice" & a).Visible = True
                If Rs.Fields("LastStopSystem").Value = True Then
                    If Rs.Fields("ReturningSystem").Value = True Then
                        Me.Controls("lblSystemMakeModel" & a).Text = "LS - " & Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value & " RETURN"
                        Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.Red
                    Else
                        Me.Controls("lblSystemMakeModel" & a).Text = "LS - " & Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value
                        Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.DarkGreen
                    End If
                    Me.Controls("lblSystemMakeModel" & a).Visible = True
                    Me.Controls("lblSystemMakeModel" & a).Font = New Font(Me.Controls("lblSystemMakeModel" & a).Font, FontStyle.Bold)
                Else
                    If Rs.Fields("ReturningSystem").Value = True Then
                        Me.Controls("lblSystemMakeModel" & a).Text = Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value & " RETURN"
                        Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.Red
                    Else
                        Me.Controls("lblSystemMakeModel" & a).Text = Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value
                        Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.Black
                    End If
                    Me.Controls("lblSystemMakeModel" & a).Visible = True
                    Me.Controls("lblSystemMakeModel" & a).Font = New Font(Me.Controls("lblSystemMakeModel" & a).Font, FontStyle.Regular)
                End If
                Rs.MoveNext()
            End If
        End If
    Next
    If Rs.EOF = False Then cmdScrollDown.Enabled = True
End If

这是我到目前为止所做的,并且在某些方面它正在起作用,但我无法通过表单元素循环工作。请帮帮我。

Public Conn As OleDbConnection
Public Rs As OleDbDataAdapter
Public DS As New DataSet
Public Sql As String
Public oControl As Label

Public Sub ConnOpenClose()
  'If Conn.State = ConnectionState.Open Then Conn.Close()
  Conn = New OleDbConnection
  Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=C:\inetpub\CompTracking.mdb"
  Conn.Open()
End Sub

Public Function FindAControl(ByVal controls As ControlCollection, ByVal toFind As String) As Control

  If controls IsNot Nothing Then
    For Each oControl As Control In controls
      If oControl.ID.Equals(toFind, StringComparison.InvariantCultureIgnoreCase) Then
        Return oControl
      ElseIf oControl.HasControls Then
        Dim oFoundControl As Control

        oFoundControl = FindAControl(oControl.Controls, toFind)
        If oFoundControl IsNot Nothing Then
          Return oFoundControl
        End If
      End If
    Next
  End If

  Return Nothing
End Function


Public Sub RefreshItems()
  lblCheckedIn.Text = "0"
  lblOnBench.Text = "0"
  lblUpdate.Text = "0"
  lblContactUs.Text = "0"
  lblFinished.Text = "0"
  lblToBeScrapped.Text = "0"
  lblLastStop.Text = "0"
  lblReturns.Text = "0"

  moveIndex = 0

  ConnOpenClose()

  Sql = "SELECT * FROM Invoices WHERE CurrentStatus<>'Checked-Out' ORDER BY INVOICES.CurrentStatusNum, INVOICES.OpenedDate,OpenedTime"
  Rs = New OleDbDataAdapter(Sql, Conn)
  Rs.Fill(DS, "Invoices")

  For i = 0 To DS.Tables("Invoices").Rows.Count - 1
    If DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Checked In" Then
      lblCheckedIn.Text = Convert.ToInt16(lblCheckedIn.Text) + 1
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "On Bench" Then
      lblOnBench.Text = Convert.ToInt16(lblOnBench.Text) + 1
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Update" Then
      lblUpdate.Text = Convert.ToInt16(lblUpdate.Text) + 1
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Contact Us" Then
      lblContactUs.Text = Convert.ToInt16(lblContactUs.Text) + 1
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "To Be Scrapped" Then
      lblToBeScrapped.Text = Convert.ToInt16(lblToBeScrapped.Text) + 1
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Finished" Then
      lblFinished.Text = Convert.ToInt16(lblFinished.Text) + 1
    End If
    If DS.Tables("Invoices").Rows(i).Item("LastStopSystem") = True Then
      lblLastStop.Text = Convert.ToInt16(lblLastStop.Text) + 1
    End If
    If DS.Tables("Invoices").Rows(i).Item("ReturningSystem") = True Then
      lblReturns.Text = Convert.ToInt16(lblReturns.Text) + 1
    End If
  Next

  lblTotalCheckedIn.Text = "Checked In: " & lblCheckedIn.Text
  lblTotalOnBench.Text = "On Bench: " & lblOnBench.Text
  lblTotalUpdate.Text = "Update: " & lblUpdate.Text
  lblTotalContactUs.Text = "Contact Us: " & lblContactUs.Text
  lblTotalToBeScrapped.Text = "To Be Scrapped: " & lblToBeScrapped.Text
  lblTotalFinished.Text = "Finished: " & lblFinished.Text
  lblTotalLastStop.Text = "LS Systems: " & lblLastStop.Text
  lblTotalReturns.Text = "Returns: " & lblReturns.Text

  ConnOpenClose()

  Sql = "SELECT * FROM Invoices WHERE CurrentStatus<>'Checked-Out' ORDER BY INVOICES.CurrentStatusNum, INVOICES.OpenedDate,OpenedTime"
  SortCategory = ""
  Rs = New OleDbDataAdapter(Sql, Conn)
  Rs.Fill(DS, "Invoices")
  For a = 1 To 20
    If ((a - 1) + moveIndex) > (DS.Tables("Invoices").Rows.Count - 1) Then
      'exceeds total number of records, display blank fields
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
      If oControl IsNot Nothing Then oControl.Text = ""
      If oControl IsNot Nothing Then oControl.Visible = False
      oControl = TryCast(FindAControl(Me.Controls, "lblInvoice" & a), Label)
      If oControl IsNot Nothing Then oControl.Text = ""
      If oControl IsNot Nothing Then oControl.Visible = False
      oControl = TryCast(FindAControl(Me.Controls, "lblSystemMakeModel" & a), Label)
      If oControl IsNot Nothing Then oControl.Text = ""
      If oControl IsNot Nothing Then oControl.Visible = False
      cmdScrollDown.Enabled = False
    Else
      If sortCategory <> DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") Then
        sortCategory = DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus")
        If DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Checked In" Then
          oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
          If oControl IsNot Nothing Then oControl.Text = "Checked In - Total: " & lblTotalCheckedIn.Text
          If oControl IsNot Nothing Then oControl.Visible = False
        ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "On Bench" Then
          oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
          If oControl IsNot Nothing Then oControl.Text = "On Bench - Total: " & lblTotalOnBench.Text
          If oControl IsNot Nothing Then oControl.Visible = False
        ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Update" Then
          oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
          If oControl IsNot Nothing Then oControl.Text = "Update - Total: " & lblTotalUpdate.Text
          If oControl IsNot Nothing Then oControl.Visible = False
        ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Contact Us" Then
          oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
          If oControl IsNot Nothing Then oControl.Text = "Contact Us - Total: " & lblTotalContactUs.Text
          If oControl IsNot Nothing Then oControl.Visible = False
        ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "To Be Scrapped" Then
          oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
          If oControl IsNot Nothing Then oControl.Text = "To Be Scrapped - Total: " & lblTotalToBeScrapped.Text
          If oControl IsNot Nothing Then oControl.Visible = False
        ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Finished" Then
          oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
          If oControl IsNot Nothing Then oControl.Text = "Finished - Total: " & lblTotalFinished.Text
          If oControl IsNot Nothing Then oControl.Visible = False
        Else
          oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
          If oControl IsNot Nothing Then oControl.Text = DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus")
          If oControl IsNot Nothing Then oControl.Visible = False
        End If
        oControl = TryCast(FindAControl(Me.Controls, "lblInvoice" & a), Label)
        If oControl IsNot Nothing Then oControl.Text = ""
        If oControl IsNot Nothing Then oControl.Visible = False
        oControl = TryCast(FindAControl(Me.Controls, "lblSystemMakeModel" & a), Label)
        If oControl IsNot Nothing Then oControl.Text = ""
        If oControl IsNot Nothing Then oControl.Visible = False
      Else
        oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
        If oControl IsNot Nothing Then oControl.Text = DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus")
        If oControl IsNot Nothing Then oControl.Visible = False
        oControl = TryCast(FindAControl(Me.Controls, "lblInvoice" & a), Label)
        If oControl IsNot Nothing Then oControl.Text = DS.Tables("Invoices").Rows(a + moveIndex).Item("InvoiceID") & " - " & DS.Tables("Invoices").Rows(a + moveIndex).Item("CustomerFName") & " " & DS.Tables("Invoices").Rows(a + moveIndex).Item("CustomerLName") & " (" & DateDiff("d", DS.Tables("Invoices").Rows(a + moveIndex).Item("OpenedDate"), Now()) & " days)"
        If oControl IsNot Nothing Then oControl.Visible = True
        oControl = TryCast(FindAControl(Me.Controls, "lblSystemMakeModel" & a), Label)
        If DS.Tables("Invoices").Rows(a + moveIndex).Item("LastStopSystem") = True Then
          If DS.Tables("Invoices").Rows(a + moveIndex).Item("ReturningSystem") = True Then
            If oControl IsNot Nothing Then oControl.Text = ""
            If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.Red
          Else
            If oControl IsNot Nothing Then oControl.Text = ""
            If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.DarkGreen
          End If
          If oControl IsNot Nothing Then oControl.Visible = True
        Else
          If DS.Tables("Invoices").Rows(a + moveIndex).Item("ReturningSystem") = True Then
            If oControl IsNot Nothing Then oControl.Text = ""
            If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.Red
          Else
            If oControl IsNot Nothing Then oControl.Text = ""
            If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.Black
          End If
          If oControl IsNot Nothing Then oControl.Visible = True
        End If
        'Rs.movenext
      End If
    End If
  Next
  If (19 + moveIndex) > (DS.Tables("Invoices").Rows.Count - 1) Then cmdScrollDown.Enabled = True
End Sub

1 个答案:

答案 0 :(得分:1)

首先需要的是一种递归方法,用于在窗体的控件集合中查找特定控件:

Private Function FindAControl(ByVal controls As ControlCollection, ByVal toFind As String) As Control

    If String.IsNullOrEmpty(toFind) Then
       Throw New ArgumentException("toFind is null or empty")
    End If

    If controls IsNot Nothing Then
        For Each oControl As Control In controls
            If oControl IsNot Nothing Then
                If Not String.IsNullOrEmpty(oControl.ID) AndAlso oControl.ID.Equals(toFind, StringComparison.InvariantCultureIgnoreCase) Then
                    Return oControl
                ElseIf oControl.HasControls Then
                    Dim oFoundControl As Control

                    oFoundControl = FindAControl(oControl.Controls, toFind)
                    If oFoundControl IsNot Nothing Then
                        Return oFoundControl
                    End If
                End If
            End If
        Next
    End If

    Return Nothing
End Function

您可以将其称为:

Dim oControl As Label
oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label)
If oControl IsNot Nothing Then
    oControl.Text = ""
End If