0

我在一个 asp.net vb 项目中使用了相当陈旧的代码。我有一系列想要初始化和使用的面板,但我发现每次我想在 sub 中使用它们时都必须继续定义它们。

这就是我的意思

Partial Class Main_Test
    Inherits System.Web.UI.Page

    Public Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
        Dim attendedPanel As Panel = Me.FormView1.FindControl("attendedPanel")
        Dim didNotAttendPanel As Panel = Me.FormView1.FindControl("didNotAttendPanel")
        Dim groupOnePanel As Panel = Me.FormView1.FindControl("groupOnePanel")
        Dim groupTwoPanel As Panel = Me.FormView1.FindControl("groupTwoPanel")
        Dim groupThreePanel As Panel = Me.FormView1.FindControl("groupThreePanel")
        Dim finishPanel As Panel = Me.FormView1.FindControl("finishPanel")
        Dim didNotAttendFinishPanel As Panel = Me.FormView1.FindControl("didNotAttendFinishPanel")
    End Sub

    Protected Sub attendedPanelClick(ByVal sender As Object, ByVal e As System.EventArgs)
        'test if attended checkbox is ticked
        Dim attendedCheckbox As CheckBox
        attendedCheckbox = Me.FormView1.FindControl("AttendedOrFTACheckBox")
        If attendedCheckbox.Checked Then
            showGroupOne()
        Else
            showDidNotAttend()
        End If
    End Sub

    Public Sub showDidNotAttend()
        Dim attendedPanel As Panel = Me.FormView1.FindControl("attendedPanel")
        Dim didNotAttendPanel As Panel = Me.FormView1.FindControl("didNotAttendPanel")
        Dim groupOnePanel As Panel = Me.FormView1.FindControl("groupOnePanel")
        Dim groupTwoPanel As Panel = Me.FormView1.FindControl("groupTwoPanel")
        Dim groupThreePanel As Panel = Me.FormView1.FindControl("groupThreePanel")
        Dim finishPanel As Panel = Me.FormView1.FindControl("finishPanel")
        Dim didNotAttendFinishPanel As Panel = Me.FormView1.FindControl("didNotAttendFinishPanel")

        attendedPanel.Visible = False
        didNotAttendPanel.Visible = True
        groupOnePanel.Visible = False
        groupTwoPanel.Visible = False
        groupThreePanel.Visible = False
        finishPanel.Visible = False
        didNotAttendFinishPanel.Visible = False

    End Sub

    Public Sub showGroupOne()
        Dim attendedPanel As Panel = Me.FormView1.FindControl("attendedPanel")
        Dim didNotAttendPanel As Panel = Me.FormView1.FindControl("didNotAttendPanel")
        Dim groupOnePanel As Panel = Me.FormView1.FindControl("groupOnePanel")
        Dim groupTwoPanel As Panel = Me.FormView1.FindControl("groupTwoPanel")
        Dim groupThreePanel As Panel = Me.FormView1.FindControl("groupThreePanel")
        Dim finishPanel As Panel = Me.FormView1.FindControl("finishPanel")
        Dim didNotAttendFinishPanel As Panel = Me.FormView1.FindControl("didNotAttendFinishPanel")

        attendedPanel.Visible = False
        didNotAttendPanel.Visible = False
        groupOnePanel.Visible = True
        groupTwoPanel.Visible = False
        groupThreePanel.Visible = False
        finishPanel.Visible = False
        didNotAttendFinishPanel.Visible = False

    End Sub

所以我真正需要的是一种方法来获取 Dim 语句并将它们放在自己的函数中。

例如

 sub setDims()
  'set the panels up here
 end sub

那么有人可以告诉我我在这里做错了什么吗?

4

1 回答 1

0

You can use either global variables, a helper function that takes Booleans or just skip the variable declaration completely:

Public Sub showDidNotAttend()
    Me.FormView1.FindControl("attendedPanel").Visible = False
    Me.FormView1.FindControl("didNotAttendPanel").Visible = False
    Me.FormView1.FindControl("groupOnePanel").Visible = True
    Me.FormView1.FindControl("groupTwoPanel").Visible = False
    Me.FormView1.FindControl("groupThreePanel").Visible = False
    Me.FormView1.FindControl("finishPanel").Visible = False
    Me.FormView1.FindControl("didNotAttendFinishPanel").Visible = False
End Sub
于 2013-11-05T14:10:31.863 回答