Accessing ArrayList after redirecting my page c#

G

Guest

I was wondering if someone can help me with an web application design
problem. I have a aspx page which builds up an arraylist called addresses
and outputs the values in the arraylist items to a datagrid. I am using the
viewstate object to store the Arraylist items on the page on postback.

My PROBLEM is that I need to redirect the user to a new aspx page and on
this new page i need to be able to access the items in my arraylist. Is this
possible without using a session object?? Does the viewstate get lost when a
page is redirected?? Below is the code im using to create my arraylist and
store it in viewstate. Can i use Server.Transfer to do this?? If yes is it
recommended??
Is there a simply way of doing this which I am overlooking??

Thanks to everyone who has helped me so far and thanks to any one who can
give me advice or help me now.

Items added to arraylist using contents of textboxes. Then bound to a
datagrid.

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

}

Added to arraylist using contents of another datagrid. Then they are saved
to state and bound to my datagrid.

private void dgResults_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Address a = new Address();
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
a.Address1 = e.Item.Cells[0].Text;
addresses.Add(a);
dgSearchAddresses.DataSource = addresses;
dgSearchAddresses.DataBind();
checkArrayList();
}

This is the page load event which enables my page to keeps its state between
posts and build up the arraylist.

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();
}
}

}

This is the Address class which I use.

[Serializable]
public class Address
{
private string _address1;
public string Address1
{
get{ return _address1; }
set{ _address1 = value; }
}
private string _address2;
public string Address2
{
get{ return _address2; }
set{ _address2 = value; }
}
etc, etc............

//private string _fulladdress;
public string FullAddress
{
get
{
return _address1 + " " + _address2 + " " + _address3 + " " + _address4 + " "
+ _address5 + " " + _address6;
}
}
 
D

Dennis Myrén

There is a cool way(yes, Server.Transfer you can and will use):
Define a readonly public/internal property in the same class as your
ArrayList
that exposes this ArrayList(which has to be at least module global),example:
public ArrayList List
{
get
{
return _list;
}
}

Go to from where you want to redirect to the second page, add the simple
line
(where WebForm2.aspx is your destination):
Server.Transfer("WebForm2.aspx");

Go to WebForm2.aspx.cs, to where you want to retrieve the ArrayList from
"WebForm1" and add this code to obtain it:

WebForm1 webForm1 = base.Context.Handler as WebForm1;
if (null != webForm1) // Sorry, Jon
{
// Add the logic here, example:
foreach (object o in webForm1.List)
{
Response.Write(o.ToString());
}
}

And that is it.


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Stephen said:
I was wondering if someone can help me with an web application design
problem. I have a aspx page which builds up an arraylist called addresses
and outputs the values in the arraylist items to a datagrid. I am using
the
viewstate object to store the Arraylist items on the page on postback.

My PROBLEM is that I need to redirect the user to a new aspx page and on
this new page i need to be able to access the items in my arraylist. Is
this
possible without using a session object?? Does the viewstate get lost when
a
page is redirected?? Below is the code im using to create my arraylist and
store it in viewstate. Can i use Server.Transfer to do this?? If yes is
it
recommended??
Is there a simply way of doing this which I am overlooking??

Thanks to everyone who has helped me so far and thanks to any one who can
give me advice or help me now.

Items added to arraylist using contents of textboxes. Then bound to a
datagrid.

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

}

Added to arraylist using contents of another datagrid. Then they are saved
to state and bound to my datagrid.

private void dgResults_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Address a = new Address();
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
a.Address1 = e.Item.Cells[0].Text;
addresses.Add(a);
dgSearchAddresses.DataSource = addresses;
dgSearchAddresses.DataBind();
checkArrayList();
}

This is the page load event which enables my page to keeps its state
between
posts and build up the arraylist.

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();
}
}

}

This is the Address class which I use.

[Serializable]
public class Address
{
private string _address1;
public string Address1
{
get{ return _address1; }
set{ _address1 = value; }
}
private string _address2;
public string Address2
{
get{ return _address2; }
set{ _address2 = value; }
}
etc, etc............

//private string _fulladdress;
public string FullAddress
{
get
{
return _address1 + " " + _address2 + " " + _address3 + " " + _address4 + "
"
+ _address5 + " " + _address6;
}
}
 
G

Guest

Tried this but Im getting the following runtime error in the browser window
when I run the project: -
Exception of type System.StackOverflowException was thrown.

Have you any ideas?? Thanks so much for your help

//click event to redirect the page
private void ArrayList_Click(object sender, System.EventArgs e)
{
Server.Transfer("ArrayListItemsDisplayed.aspx");
}

//added to my Debtor Enquiry page (WebForm1) at the top
public ArrayList addresses
{
get
{
return addresses;
}
}

//Trying to write the values on the page load event
private void Page_Load(object sender, System.EventArgs e)
{
WebForm1 debtor_enquiry = base.Context.Handler as WebForm1;
if (null != debtor_enquiry)
{
// Add some logic here
foreach (object o in debtor_enquiry.addresses)
{
Response.Write(o.ToString());
}
}
}


Dennis Myrén said:
There is a cool way(yes, Server.Transfer you can and will use):
Define a readonly public/internal property in the same class as your
ArrayList
that exposes this ArrayList(which has to be at least module global),example:
public ArrayList List
{
get
{
return _list;
}
}

Go to from where you want to redirect to the second page, add the simple
line
(where WebForm2.aspx is your destination):
Server.Transfer("WebForm2.aspx");

Go to WebForm2.aspx.cs, to where you want to retrieve the ArrayList from
"WebForm1" and add this code to obtain it:

WebForm1 webForm1 = base.Context.Handler as WebForm1;
if (null != webForm1) // Sorry, Jon
{
// Add the logic here, example:
foreach (object o in webForm1.List)
{
Response.Write(o.ToString());
}
}

And that is it.


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Stephen said:
I was wondering if someone can help me with an web application design
problem. I have a aspx page which builds up an arraylist called addresses
and outputs the values in the arraylist items to a datagrid. I am using
the
viewstate object to store the Arraylist items on the page on postback.

My PROBLEM is that I need to redirect the user to a new aspx page and on
this new page i need to be able to access the items in my arraylist. Is
this
possible without using a session object?? Does the viewstate get lost when
a
page is redirected?? Below is the code im using to create my arraylist and
store it in viewstate. Can i use Server.Transfer to do this?? If yes is
it
recommended??
Is there a simply way of doing this which I am overlooking??

Thanks to everyone who has helped me so far and thanks to any one who can
give me advice or help me now.

Items added to arraylist using contents of textboxes. Then bound to a
datagrid.

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

}

Added to arraylist using contents of another datagrid. Then they are saved
to state and bound to my datagrid.

private void dgResults_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Address a = new Address();
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
a.Address1 = e.Item.Cells[0].Text;
addresses.Add(a);
dgSearchAddresses.DataSource = addresses;
dgSearchAddresses.DataBind();
checkArrayList();
}

This is the page load event which enables my page to keeps its state
between
posts and build up the arraylist.

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();
}
}

}

This is the Address class which I use.

[Serializable]
public class Address
{
private string _address1;
public string Address1
{
get{ return _address1; }
set{ _address1 = value; }
}
private string _address2;
public string Address2
{
get{ return _address2; }
set{ _address2 = value; }
}
etc, etc............

//private string _fulladdress;
public string FullAddress
{
get
{
return _address1 + " " + _address2 + " " + _address3 + " " + _address4 + "
"
+ _address5 + " " + _address6;
}
}
 
D

Dennis Myrén

I think i see the problem here;
You have defined a public property addresses
and in the get clause you are returning addresses.
This leads to an infinite loop, since public property addresses
then is actually calling itself, which is in turn calling itself, and so on.
If the private ArrayList variable is also called addresses, i suggest
you rename the property to Addresses(capital A) or something else.



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Stephen said:
Tried this but Im getting the following runtime error in the browser
window
when I run the project: -
Exception of type System.StackOverflowException was thrown.

Have you any ideas?? Thanks so much for your help

//click event to redirect the page
private void ArrayList_Click(object sender, System.EventArgs e)
{
Server.Transfer("ArrayListItemsDisplayed.aspx");
}

//added to my Debtor Enquiry page (WebForm1) at the top
public ArrayList addresses
{
get
{
return addresses;
}
}

//Trying to write the values on the page load event
private void Page_Load(object sender, System.EventArgs e)
{
WebForm1 debtor_enquiry = base.Context.Handler as WebForm1;
if (null != debtor_enquiry)
{
// Add some logic here
foreach (object o in debtor_enquiry.addresses)
{
Response.Write(o.ToString());
}
}
}


Dennis Myrén said:
There is a cool way(yes, Server.Transfer you can and will use):
Define a readonly public/internal property in the same class as your
ArrayList
that exposes this ArrayList(which has to be at least module
global),example:
public ArrayList List
{
get
{
return _list;
}
}

Go to from where you want to redirect to the second page, add the simple
line
(where WebForm2.aspx is your destination):
Server.Transfer("WebForm2.aspx");

Go to WebForm2.aspx.cs, to where you want to retrieve the ArrayList from
"WebForm1" and add this code to obtain it:

WebForm1 webForm1 = base.Context.Handler as WebForm1;
if (null != webForm1) // Sorry, Jon
{
// Add the logic here, example:
foreach (object o in webForm1.List)
{
Response.Write(o.ToString());
}
}

And that is it.


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Stephen said:
I was wondering if someone can help me with an web application design
problem. I have a aspx page which builds up an arraylist called
addresses
and outputs the values in the arraylist items to a datagrid. I am
using
the
viewstate object to store the Arraylist items on the page on postback.

My PROBLEM is that I need to redirect the user to a new aspx page and
on
this new page i need to be able to access the items in my arraylist. Is
this
possible without using a session object?? Does the viewstate get lost
when
a
page is redirected?? Below is the code im using to create my arraylist
and
store it in viewstate. Can i use Server.Transfer to do this?? If yes
is
it
recommended??
Is there a simply way of doing this which I am overlooking??

Thanks to everyone who has helped me so far and thanks to any one who
can
give me advice or help me now.

Items added to arraylist using contents of textboxes. Then bound to a
datagrid.

protected void cmdExcAdrContinue_Click(object sender, System.EventArgs
e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

Address newAddress = new Address();
newAddress.Address1 = this.txtAddress1.Text.Trim();
newAddress.Address2 = this.txtAddress2.Text.Trim();
newAddress.Address3 = this.txtAddress3.Text.Trim();
newAddress.Address4 = this.txtAddress4.Text.Trim();
newAddress.Address5 = this.txtAddress5.Text.Trim();
newAddress.Address6 = this.txtAddress6.Text.Trim();

addresses.Add(newAddress);
ViewState["Addresses"] = addresses;

this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();

//clear down the textboxes
this.txtAddress1.Text = "";
this.txtAddress2.Text = "";
this.txtAddress3.Text = "";
this.txtAddress4.Text = "";
this.txtAddress5.Text = "";
this.txtAddress6.Text = "";

}

Added to arraylist using contents of another datagrid. Then they are
saved
to state and bound to my datagrid.

private void dgResults_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Address a = new Address();
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
a.Address1 = e.Item.Cells[0].Text;
addresses.Add(a);
dgSearchAddresses.DataSource = addresses;
dgSearchAddresses.DataBind();
checkArrayList();
}

This is the page load event which enables my page to keeps its state
between
posts and build up the arraylist.

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList();
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
this.dgSearchAddresses.DataSource = addresses;
this.dgSearchAddresses.DataBind();
}
}

}

This is the Address class which I use.

[Serializable]
public class Address
{
private string _address1;
public string Address1
{
get{ return _address1; }
set{ _address1 = value; }
}
private string _address2;
public string Address2
{
get{ return _address2; }
set{ _address2 = value; }
}
etc, etc............

//private string _fulladdress;
public string FullAddress
{
get
{
return _address1 + " " + _address2 + " " + _address3 + " " + _address4
+ "
"
+ _address5 + " " + _address6;
}
}
 

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

Top