2012-01-05 61 views
2

好的,所以我最近從這個窗體上的一些人那裏得到了一些幫助,學習如何在VB.Net中通過類似名稱的窗體控件進行循環。我現在正在將整個應用程序轉換爲Web應用程序,並且我陷入了相同的位置。任何人都可以幫我弄清楚我在這裏做錯了什麼。我知道這一定很簡單,但它暗指着我。我附上,在VB.Net工作代碼:ASP.NET 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 
+0

哦存儲過程名爲「sp_SB-Invoices」的數據庫就是這個SQL語句:SELECT * FROM Invoices WHERE CurrentStatus <>'Checked-out'ORDER BY INVOICES.CurrentStatusNum,INVOICES.OpenedDate,OpenedTime WITH OWNERACCESS OPTION; – Russell 2012-01-05 22:54:32

回答

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 
+0

我收到你寫的那個函數的警告,它在第9行的oFoundControl.Controls下面給了我一個扭曲的行,警告「Variable oFoundControl'在它被賦值之前使用。在運行時結果。「 - 當我編譯並運行它時,第4行出現錯誤,NullReferenceException未被用戶代碼處理。這怎麼解決。該函數被調用在這個問題:TryCast(FindAControl(Me.Controls,「lblStatus」&a),Label).Text =「」 – Russell 2012-01-06 01:23:01

+1

對不起,有一個錯字在代碼中。我已經修復了答案,但是您可以將帶下劃線的名稱更改爲「oControl」。 – 2012-01-06 01:24:49

+0

好吧,現在這條扭曲的線條消失了,但是在編譯和運行時,它會在If oControl.ID.Equals(toFind,StringComparison.InvariantCultureIgnoreCase)處拋出另一個錯誤。 - 錯誤再次是「NullReferenceException未被用戶代碼處理」任何其他想法? – Russell 2012-01-06 02:56:37

相關問題