Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing variables between web pages with VB.Net

Posted on 2006-06-29
8
Medium Priority
?
356 Views
Last Modified: 2010-04-23
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.FindControl("TextBox1"), _
        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
0
Comment
Question by:swtiley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 14

Expert Comment

by:ptakja
ID: 17014809
How about storing the variables in Session objects?

' 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
0
 
LVL 5

Expert Comment

by:t_itanium
ID: 17015746
also you can use cookies to achieve that
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 2000 total points
ID: 17016007
Hi swtiley
I have had the same issues myself, and after a lot of deliberation I found out a few things about it.
The way of accessing
    PreviousPage.FindControl("TextBox1")
actually depends on where your TextBox is placed, is it directly under the form tag? do you have a Content Tag ? Are you using ContentPlaceHolder ?
Its not very well documented yet, so people are learning it the hard way (like I did)
ultimately I was able to access the textBox like this
   PreviousPage.FindControl("MyContentPlaceHolder").FindControl("textBox1")

So the bottomline is check where your TextBox is "contained" and try to refer it in the Controls collection of that "container"

And if you want a little fun, try putting a Theme using Master Pages, then control hierechy changes further, now you would require one more level of "Container" access,
Also try using the Request Collection, Microsoft says Request.Form collection should give you the access to the TextBox so virtually you should get it like this
   Request.Form("TextBox1")
it works for a normal form but attach a Master Page and it won't, I have posted a query at ASP.NET forum also waiting for some explanation for this wierd behaviour
       http://forums.asp.net/thread/1328168.aspx

However as an End Note to it, the best way to use PreviousPage is by Registering it   <% @ PreviousPageType VirtualPath="..." %>
and then having Properties exposing the fields so that we access the properties in the posted page and not the TextBox like
      PreviousPage.UserName (in case the TextBox1 was used to get User Name and so on) where UserName is a property that returns TextBox1.Text


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Expert Comment

by:arif_eqbal
ID: 17016033
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


0
 
LVL 6

Expert Comment

by:Nandakumar Sakthivel
ID: 17016064
Hi,
   You can use Server.Transfer("<TargetPage>") and can get the value in the second page as
             Request.Form("ControlName")

Nanda
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17016094
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
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 17016118
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).
0
 
LVL 1

Author Comment

by:swtiley
ID: 17024969
Thanks for all you help...
...Steve
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question