ASP.NET页面中的变量范围

时间:2013-11-05 13:12:24

标签: asp.net vb.net global-variables

我在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语句并将它们放在自己的函数中。

e.g。

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

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

1 个答案:

答案 0 :(得分:0)

你可以使用全局变量,一个带有布尔值的辅助函数,或者只是完全跳过变量声明:

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