Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing variables in ASP.Net

BezerkRogue's Avatar
Member
 
Join Date: Oct 2007
Posts: 68
#1: Oct 22 '07
This is the most fundamental action I am sure, but I can't seem to make it happen.

I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have some scripts that run at the server for the form so I can't disable that statement. I followed Microsoft's instructions(I know..not the best thing to do) but still can't get the variables to pass.

I created a class and set up an @reference statement, created property declarations to get the variables in the source page and set the server.transfer instruction to pass to the next page. The results....nothing. nada, zilch.

Can anyone help me out here?

Here's the inline script for the source page:
'Variable declaration for class object to encapsulate variables for page transfer
Dim fp As FirstPageClass

'Read property for Shift variable
Public ReadOnly Property Shift() As String
Get
Return SHIFT_ID.Text
End Get
End Property

'Read property for Date variable
Public ReadOnly Property SelDate() As String
Get
Return OccDate.SelectedDate
End Get
End Property

'Command string to pass the variables to next page
Sub cmdSubmit_OnClick(ByVal Sender As Object, ByVal e As EventArgs)
Server.Transfer("ChangeMPRep.aspx")

Here's the inline script for the receiving page:
Dim fp As FirstPageClass
Sub Page_Load()
Dim fp
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Oct 22 '07

re: Passing variables in ASP.Net


You could use the Session object for this?
BezerkRogue's Avatar
Member
 
Join Date: Oct 2007
Posts: 68
#3: Oct 22 '07

re: Passing variables in ASP.Net


Is this the correct code to put in the VB file

Source page:

Session.Add("SelDate", OccDate.SelectedDate)
Session.Add("Shift", SHIFT_ID)

Receiving page: (Displaying variable in labels)

lblSelDate = Session("SelDate")
lblShift = Session("Shift")

I think that I might be missing something in between.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Oct 22 '07

re: Passing variables in ASP.Net


That looks good, it's important to test for null though.
Expand|Select|Wrap|Line Numbers
  1. if (Session("SelDate") !=null)
  2. {
  3.    lblSelDate = Session("SelDate")
  4. }
  5. if (Session("Shift") !=null)
  6. {
  7.    lblShift = Session("Shift")
  8. }
  9.  
BezerkRogue's Avatar
Member
 
Join Date: Oct 2007
Posts: 68
#5: Oct 22 '07

re: Passing variables in ASP.Net


Got it. Would using context work as well? I am limited on server resources and this is shaping up to be a very busy site.

context.items.add("name", value)
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Oct 22 '07

re: Passing variables in ASP.Net


I am unfamiliar with "context" objects. Sorry.
nateraaaa's Avatar
Expert
 
Join Date: May 2007
Location: Illinois
Posts: 663
#7: Oct 22 '07

re: Passing variables in ASP.Net


Quote:

Originally Posted by BezerkRogue

Got it. Would using context work as well? I am limited on server resources and this is shaping up to be a very busy site.

context.items.add("name", value)

You could also pass the variable as a querystring value.
page.aspx?id=variablevalue

Nathan
Reply