【问题标题】:ASP.NET VB.NET Looping through similarly named form controlsASP.NET VB.NET 通过类似命名的表单控件循环
【发布时间】:2012-01-05 22:52:45
【问题描述】:

好的,所以我最近从这个表单上的一些人那里得到了一些帮助,学习如何在 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

【问题讨论】:

  • 哦,数据库中名为“sp_SB-Invoices”的存储过程是这样的 SQL 语句:SELECT * FROM Invoices WHERE CurrentStatus'Checked-Out' ORDER BY INVOICES.CurrentStatusNum, INVOICES.OpenedDate,OpenedTime带有所有者访问选项;

标签: asp.net vb.net


【解决方案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

【讨论】:

  • 抱歉,代码中有错字。我已经修复了答案,但是您可以将带下划线的名称更改为oControl
  • 这非常非常奇怪。我只能想到两个原因:1)toFind 什么都不是(如果是这种情况,我已经更新了答案以抛出不同的异常); 2)由于某些未知的原因,控制什么都不是(我想不出这会如何,但我在答案中添加了一个案例)。
  • 是否抛出参数异常(tofind 为空)?我想知道那里是否有没有ID的控件?我不认为 asp.net 允许这样做,但这是我能想到的唯一其他事情。更新答案并检查。
  • 好的,这有助于缩小范围。在调用函数的行上还要检查 2 件事:1)a 的值是多少,2)Me.Controls 是否有效?
  • 抱歉,我不确定还有什么建议。我在这里试过了,它工作正常。您可以尝试在页面顶部添加 Option Explicit On 和 Option Strict On 以查看是否有任何冲突。否则,我完全被难住了,这种情况并不经常发生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
相关资源
最近更新 更多