ASP.NET 页面中的变量范围



我在一个 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

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

您可以使用全局变量、采用布尔值的帮助程序函数,或者完全跳过变量声明:

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

相关内容

  • 没有找到相关文章

最新更新