Error passing values from one pahge to another

G

Guest

Hi,
I have two froms (form1 and form2). I want to be able to pass values from
form 1 to form2 and be able to use those values leter in form2. This is my
code for form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Server.Transfer("form2.aspx", True)
End Sub

Public ReadOnly Property Property1() As String
Get
Return DropDownList1.SelectedItem.Text
End Get
End Property


This is my code for form2



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then

sourcepage = CType(Context.Handler, transsend)

TextBox1.Text = sourcepage.Property1

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

sourcepage = CType(Context.Handler, transsend)

TextBox2.Text = sourcepage.Property1


End Sub


The form load works perfect on form2 but when I click the button1 on form2 I
get the error

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 45: Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
Line 46:
Line 47: sourcepage = CType(Context.Handler, transsend)
Line 48:
Line 49: TextBox2.Text = sourcepage.Property1

Any ideas?
 
S

Steve C. Orr [MVP, MCSD]

Here's a nice, simple way to pass values from one page to another:
(VB.NET code)

'Add data to the context object before transferring
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")

Then, in WebForm2.aspx:

'Grab data from the context property
Dim x as Integer = CType(Context.Items("myParameter"),Integer)

Of course there are a number of ways to pass values from one page to
another, such as using the querystring, cookies, session,
context, saving to a temporary table in the database between each page, etc.
You'll have to decide which technique is best for your application.
Here are several good articles on the subject to help you decide.
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/default.aspx

http://www.aspalliance.com/kenc/passval.aspx

http://www.dotnetbips.com/displayarticle.aspx?id=79
 
W

William F. Robertson, Jr.

When you use Server.Transfer the Handler is only set to the "transferer" on
the first request. When the user hits a button on your second page and
causes a postback, the first page that did the transferring is gone forever.

You will to persist Property1 if you would access to it on a postback.

HTH,

bill
 
G

Guest

Hi,
Looking at the example

http://www.dotnetbips.com/displayarticle.aspx?id=79

That's still using the session instead of the context. I tries using the
context but The only way the data ia available after form.transfer is at form
laod of the second form. I am looking for a way to store it by using least
amount of memory and not in database and not on client so cookies are out.

Thanks
 
G

Guest

How do I persist property1?

William F. Robertson said:
When you use Server.Transfer the Handler is only set to the "transferer" on
the first request. When the user hits a button on your second page and
causes a postback, the first page that did the transferring is gone forever.

You will to persist Property1 if you would access to it on a postback.

HTH,

bill
 
G

Guest

My objective is. I have two forms one with customer data and second with
shipping data. After the user fills the customer data and select next the
data is transferred to second form. When second form is filled the data is
sent to database. Now each forms have 15 textboxes so that would be a total
of 30 variables when transferred to form2. That's why I am afraid of using
the sesion. If 4 users are using that will be a total of 120 variables in
session
 
S

Steve C. Orr [MVP, MCSD]

OK, I'd suggest using a single form with two panels.
Only one panel is visible at a time. The first panel holds the customer
controls, and the second panel holds the shipping controls.
That way all the controls persist between postback automatically and it's
fairly light on server RAM usage. The tradeoff is it sucks up slightly more
bandwidth, but hey, you can't have everything.
 
W

William F. Robertson, Jr.

Sorry about the weekend delay.

Place this property on your page 2.

Private Property Property1FromPage1()
Get
Return CType(ViewState("Property1FromPage1"), String)
End Get
Set(ByVal Value)
ViewState("Property1FromPage1") = Value
End Set
End Property


In your page load, when not postback, you will set this property:

Page_Load
Property1FromPage1 = CType(Context.Handler, transsend).Property1

Property1FromPage1 will always contain the value from the first page, and
you can use this value on all subsequent postbacks.

bill
 
G

Guest

Hi,
How long does session variable stays in server memory after session.remove?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top