通过button.click处理程序使用时,变量会丢失范围

时间:2013-01-22 02:43:39

标签: asp.net vb.net scope

当我在Me.Load调用我的函数时,一切正常,但是当我仅在按钮点击事件中触发我的函数时,所有内部变量都会丢失它们的设置值。我刚刚开始使用VB.NET,所以我不完全了解范围本质。

这是我的代码:

Public Class WorkflowForward
    Inherits FileBound.Web.FBBasePage

    ' Private decalrations
    Private cfile_id As String = ""                             ' Current files ID
    Private FBProject As Project = Nothing                      ' FileBound Project object
    Private cmd As String = ""                                  ' SQL query string
    Private FBFile As FileBound.File = Nothing                  ' Filebound file holder
    Private FBRoutes As RouteCollection = Nothing               ' FileBound route collection holder


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Set SqlDataSource connection string
        FBSqlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("FB_0371tcc").ConnectionString

        If Not Me.IsPostBack Then
            ' Set values to variables
            Page_setup() ' Here I set values to variabels
            setRoute_debug("Admin1 Test")

        End If


    End Sub

    Protected Sub sendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles sendEmail.Click
        ' CAN I HAZ SOME TEST????
        'setRoute_debug(emailTo.SelectedValue.ToString)

        Response.Write("<script>" & Me.FBRoutes.TotalCount.ToString & "</script>")

    End Sub

    Private Sub setRoute_debug(ByVal route_to As String)

        ' IF WE HAZ SOME ITEMS FINISH THEM MUHUHAHHAHAHA
        If FBBusiness.LoggedInUser.RoutedItems.TotalCount > 0 Then
            For Each item As RoutedItem In FBBusiness.LoggedInUser.RoutedItems
                item.Complete("Completed!")
            Next
        End If




        ' DEBUG MODE #YOLO
        ' I'VE DONE IT I CAN HAZ ROUTEZ
        For Each r As Route In Me.FBRoutes
            If r.Name = route_to Then
                For Each doc As FileBound.Document In FBFile.Documents
                    doc.Route(r.ID)
                Next

            End If
        Next


    End Sub

    Private Sub Page_setup()
    ' Some code that sets values
    End Sub
End Class

当我点击按钮时会抛出此错误:

Object reference not set to an instance of an object. 
Line 46:         Response.Write("<script>" & Me.FBRoutes.TotalCount.ToString & "</script>")

1 个答案:

答案 0 :(得分:1)

点击按钮作为asp.net页面将显示为PostBack

您只在

中设置页面
If Not Me.IsPostBack

这适用于asp.net控件,因为它们使用ViewState在PostBacks中保留其状态。

单击按钮时,服务器会收到一个新请求,并创建一个新的Page对象。您没有为此对象设置变量。

在每个PostBack上设置页面级变量,例如

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' Set SqlDataSource connection string
    FBSqlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("FB_0371tcc").ConnectionString

    Page_setup() ' Here I set values to variables

    If Not Me.IsPostBack Then
        'Set Page level control values like TextBox values
    End If
End Sub