swtiley
asked on
Passing variables between web pages with VB.Net
Hi - I am trying to pass a number of variables from the first web page in my web-site to a second page. I got the following code from M/S help pages, which works fine with a basic web design, but my site uses a master page on both the web pages I mentioned before, the code then fails to work.
-------------------------- ---------- ---------- -----
Microsoft Help
-------------------------- ---------- ---------- -----
If Not Page.PreviousPage Is Nothing Then
Dim SourceTextBox As TextBox
SourceTextBox = CType(PreviousPage.FindCon trol("Text Box1"), _
TextBox)
If Not SourceTextBox Is Nothing Then
Label1.Text = SourceTextBox.Text
End If
End If
-------------------------- ---------- ---------- ----
I am not particularly interested in why it fails, I really just need someone to point me in the right direction as to how to pass variables from the first page to the second page.
Many thanks for any help
...Steve
P.S. I would prefer not to pass the variables by reference in the second page's URL
--------------------------
Microsoft Help
--------------------------
If Not Page.PreviousPage Is Nothing Then
Dim SourceTextBox As TextBox
SourceTextBox = CType(PreviousPage.FindCon
TextBox)
If Not SourceTextBox Is Nothing Then
Label1.Text = SourceTextBox.Text
End If
End If
--------------------------
I am not particularly interested in why it fails, I really just need someone to point me in the right direction as to how to pass variables from the first page to the second page.
Many thanks for any help
...Steve
P.S. I would prefer not to pass the variables by reference in the second page's URL
also you can use cookies to achieve that
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And sorry missed out your actual problem
That also is best solved by registering the first page, say you have 2 pages Page1.aspx and Page2.aspx
then on Page2 have this in the Source at the top
<% @ PreviousPageType VirtualPath="Page1.aspx" %>
Now in Page1 have a property
Public ReadOnly Property MyPassedValue() As String
Get
Return yourLocalVariable 'This can be TextBox1.Text as well
End Get
End Property
Then on Page2
If Not Page.PreviousPage Is Nothing Then
Label1.Text = PreviousPage.MyPassedValue
End If
That also is best solved by registering the first page, say you have 2 pages Page1.aspx and Page2.aspx
then on Page2 have this in the Source at the top
<% @ PreviousPageType VirtualPath="Page1.aspx" %>
Now in Page1 have a property
Public ReadOnly Property MyPassedValue() As String
Get
Return yourLocalVariable 'This can be TextBox1.Text as well
End Get
End Property
Then on Page2
If Not Page.PreviousPage Is Nothing Then
Label1.Text = PreviousPage.MyPassedValue
End If
Hi,
You can use Server.Transfer("<TargetPa ge>") and can get the value in the second page as
Request.Form("ControlName" )
Nanda
You can use Server.Transfer("<TargetPa
Request.Form("ControlName"
Nanda
Hi,
Store the variables in a XML file and call the other page,retreive the values from that XML file.
Otherwise use can use database.
--pradeep
Store the variables in a XML file and call the other page,retreive the values from that XML file.
Otherwise use can use database.
--pradeep
arif_eqbal suggestion is the way to go, it's called a cross page postback. Just thouht i would add my 2 cents and say that if the public properties do not show up in the target page then compile the entire project (sometimes just compiling the source page will do) and on the odd occasion opening the .aspx of the target page with the previous page directive will update the environment and make the properties available in PreviousPage. Also you will need to save the values into viewstate after the first hit because any other postbacks on the page will end up with a previous page of the target page instead of the source page, to facilitate that you can use IsCrossPagePostBack (similar in function to IsPostBack).
ASKER
Thanks for all you help...
...Steve
...Steve
' Put var into session object:
Session("MyTextbox1") = Textbox1.Text
Pull var from session:
strText = CStr(Session("MyTextBox1")
To clear session object:
Session("MyTextbox1") = Nothing
Check to see if session object exists:
If Not Session("MyTextBox1") Is Nothing Then
strText = Cstr(Session("MyTextBox1")
End If