Passing value between pages

G

Guest

Hi

I would like to pass values/data from Page1 to Page 2, then capture values
on page 2 and return back to Page 1 with the values from page2. Can you pls
guide me on how to do this ?

I tried Server.transfer to pass values from Page1 to Page2, but from page2
am unable to return to Page 1 or any other page with the values picked from
Page2.

Thanks
gkr
 
S

Stephen Adam

This might be a bit old skool but in my current project I need to store the
state of a menu while a user is on another page so it can be rebuilt when
they return.

I am using Text Boxes to store the values I want, then I on the receiving
page I can use Request.Form["ItemID"] to get the values, I can easly check
if there are any values by looking for the empty string. This is simple fast
and bullet proof + I make the texboxes invisible through CSS but it is easy
to remove the style and see eaxctly which values are being passed - handy in
some situations.


Of course the nice way of doing it is by using getters for the transmitting
page, then you can you use Context.Handler to try and cast the transmitting
page in the receiving page and access its values via its getters. You may
need to use a try catch clause with this in case the user hasnt visiting the
transmitting page. ITs all covered far more eliqently and completely in the
article below. Good luck and i were you id go for the getter approach :)


http://www.codeproject.com/aspnet/TransferingValues.asp
 
G

Guest

Hi Stephen

I tried the Get property but facing issues when trying to return control
from Page to Page 1. Am not sure what is going wrong.

The scenario that I would like to create is
Page 1 click button --> pass values --> Page 2 (capture passed value)
Page 2 click button --> pass values --> Page 1 (capture passed value)

Attached below is the code snippet that I am using :
WebForm1.aspx ( PAGE 1)
public string Property1
{
get
{
return TextBox1.Text;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
}
else
{
WebForm2 wf2 = (WebForm2) Context.Handler;
Label1.Text = wf2.Property2;
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("Webform2.aspx");
}

WebForm2.aspx ( PAGE 2)
public string Property2
{
get
{
return Label1.Text;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
WebForm1 sourcepage = (WebForm1) Context.Handler;
Label1.Text = sourcepage.Property1;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("Webform1.aspx",true);
}

The above code worked fine when I navigate from Page 1 to Page 2, but when
it tries to navigate back to page1 - it fails !

Can you pls help ?

Thanks
Stephen Adam said:
This might be a bit old skool but in my current project I need to store the
state of a menu while a user is on another page so it can be rebuilt when
they return.

I am using Text Boxes to store the values I want, then I on the receiving
page I can use Request.Form["ItemID"] to get the values, I can easly check
if there are any values by looking for the empty string. This is simple fast
and bullet proof + I make the texboxes invisible through CSS but it is easy
to remove the style and see eaxctly which values are being passed - handy in
some situations.


Of course the nice way of doing it is by using getters for the transmitting
page, then you can you use Context.Handler to try and cast the transmitting
page in the receiving page and access its values via its getters. You may
need to use a try catch clause with this in case the user hasnt visiting the
transmitting page. ITs all covered far more eliqently and completely in the
article below. Good luck and i were you id go for the getter approach :)


http://www.codeproject.com/aspnet/TransferingValues.asp


gkr said:
Hi

I would like to pass values/data from Page1 to Page 2, then capture values
on page 2 and return back to Page 1 with the values from page2. Can you pls
guide me on how to do this ?

I tried Server.transfer to pass values from Page1 to Page2, but from page2
am unable to return to Page 1 or any other page with the values picked from
Page2.

Thanks
gkr
 
G

Guest

Hi Stephen

I tried the Get property but facing issues when trying to return control
from Page to Page 1. Am not sure what is going wrong.

The scenario that I would like to create is
Page 1 click button --> pass values --> Page 2 (capture passed value)
Page 2 click button --> pass values --> Page 1 (capture passed value)

Attached below is the code snippet that I am using :
WebForm1.aspx ( PAGE 1)
public string Property1
{
get
{
return TextBox1.Text;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
}
else
{
WebForm2 wf2 = (WebForm2) Context.Handler;
Label1.Text = wf2.Property2;
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("Webform2.aspx");
}

WebForm2.aspx ( PAGE 2)
public string Property2
{
get
{
return Label1.Text;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
WebForm1 sourcepage = (WebForm1) Context.Handler;
Label1.Text = sourcepage.Property1;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("Webform1.aspx",true);
}


The above code worked fine when I navigate from Page 1 to Page 2, but when
it tries to navigate back to page1 - it fails !

Can you pls help ?

Thanks

Stephen Adam said:
This might be a bit old skool but in my current project I need to store the
state of a menu while a user is on another page so it can be rebuilt when
they return.

I am using Text Boxes to store the values I want, then I on the receiving
page I can use Request.Form["ItemID"] to get the values, I can easly check
if there are any values by looking for the empty string. This is simple fast
and bullet proof + I make the texboxes invisible through CSS but it is easy
to remove the style and see eaxctly which values are being passed - handy in
some situations.


Of course the nice way of doing it is by using getters for the transmitting
page, then you can you use Context.Handler to try and cast the transmitting
page in the receiving page and access its values via its getters. You may
need to use a try catch clause with this in case the user hasnt visiting the
transmitting page. ITs all covered far more eliqently and completely in the
article below. Good luck and i were you id go for the getter approach :)


http://www.codeproject.com/aspnet/TransferingValues.asp


gkr said:
Hi

I would like to pass values/data from Page1 to Page 2, then capture values
on page 2 and return back to Page 1 with the values from page2. Can you pls
guide me on how to do this ?

I tried Server.transfer to pass values from Page1 to Page2, but from page2
am unable to return to Page 1 or any other page with the values picked from
Page2.

Thanks
gkr
 
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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top