How to refer to a variable on another webform

  • Thread starter Thread starter Tor Inge Rislaa
  • Start date Start date
T

Tor Inge Rislaa

How to refer to a variable on another webform

Hi, I have a Webform1 with a public variable that is assigned a value before
my Webform2 is displayed. In the Page_Load procedure of Webform2 I want to
evaluate the value of the variable of Webform1.

Code on Webform1

Public MyVar1 As Integer

Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.ServerClick

MyVar1 = 1
Server.Transfer("Webform2.aspx")

End Sub


Code on Webform2

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim Myvar2 As Integer
Myvar2 = (Myvar1 from Webform1... Any Ideas?)

End Sub

T I Rislaa
 
You can't. Once the webform1 finishes execution, it ceases to exist.

You will have to store the value in session or pass it in the query string,
or place it elsewhere where both forms can access to it.
 
Open the Visual Studio.NET help....and type in the
following in the address bar.....

ms-
help://MS.VSCC/MS.MSDNVS/vbcon/html/vbtskPassingValuesBetwe
enWebFormsPages.htm

It describes how to pass values in between web forms and
of course you can always use query strings too.

Thanks,

Tap
 
That's not correct.

You can use server.transfer as your code is beginning to. The next step is
to recreate the first page on the second. You will need to do a CType on the
context object that is passed using server.transfer.

Dim Page1 As Page1 = CType(Context, Page1)

The important thing is that if you can arrive at a page2 from more pages
than page1 this code will error out if you try to make say, page3, into
page1.

I have some sample code in the code library on my website,
www.aboutfortunate.com for determining which page a context object came
from. Just search for: "get page name".

I hope this helps.

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Back
Top