Question Public variables in aspx.vb

raysefo

Well-known member
Joined
Jul 21, 2010
Messages
207
Programming Experience
Beginner
Hi,

I declared public variables in my aspx.vb.
VB.NET:
Partial Class PendingApproval
    Inherits System.Web.UI.Page
    
    Public formType As Integer
    Public taskID As String
    Public check As Integer = 0
    Public creator As String
    Public formID As String

then in my GridView1_RowCommand Sub, i set values from aspx. Everything works fine so far.
VB.NET:
        Dim rowIndex As Int32 = Convert.ToInt32(e.CommandArgument.ToString())
        formID = GridView1.DataKeys(rowIndex).Values("EformID").ToString()
        formType =  Convert.ToInt32(GridView1.DataKeys(rowIndex).Values("EformType").ToString())
        taskID = GridView1.DataKeys(rowIndex).Values("TaskID").ToString()


        check = 1

But when i try to use those variables in another Sub;it says they are NULL???
VB.NET:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim task As New Task
        With task
            .FormType = formType

        End With
        
    End Sub

How can i set value to a variable and then use it from another Sub?

thanks in advance.

Best regards
 
You can store the values in a property ... e.g.

VB.NET:
    Public Property TaskID() As String
        Get
            Dim id As String = CType(ViewState("taskid"), String)
            If id IsNot Nothing Then
                Return id
            Else
                Return String.Empty
            End If
        End Get
        Set(ByVal value As String)
            ViewState("taskid") = value
        End Set
    End Property

to set the TaskID property just add the value to the viewstate e.g.

VB.NET:
ViewState.Add("taskid", GridView1.DataKeys(rowIndex).Values("TaskID").ToString)

To get the value from another method/routine just call the TaskID property e.g.

VB.NET:
Response.Write(TaskID)
 
Back
Top