473,472 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Postback problem

I have the following code working in order to create an array list and
populate a datagrid however everytime i click my button the first item in the
array and the first row in the datagrid are overwritten. I think this is some
sort of post back problem but i can't seem to figure out how to solve it. Can
someone help me with this please.

private ArrayList addresses;

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

if(!(ViewState["Addresses"]==null))
{
this.addresses = (ArrayList)ViewState["Addresses"];
}
else
{
this.addresses = new ArrayList();
}

}

private void Button1_Click(object sender, System.EventArgs e)
{

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
newAddress.Address3 = this.TextBox3.Text.Trim();
newAddress.Address4 = this.TextBox4.Text.Trim();
newAddress.Address5 = this.TextBox5.Text.Trim();
newAddress.Address6 = this.TextBox6.Text.Trim();

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

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

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
this.TextBox4.Text = "";
this.TextBox5.Text = "";
this.TextBox6.Text = "";
this.TextBox1.Text = "";
}
Nov 16 '05 #1
4 12673
Stephen,

You are correct, it is to do with postbacks. What you need to remember is
that every time your page is loaded, your page class is recreated. This means
that any data stored in the class itself is recreated. No class data will
persist between page accesses (whether initial access or postback).

This will be the case with your addresses ArrayList.

To resolve the problem you need to:
1) create your empty ArrayList when the page is first loaded (i.e. when
IsPostBack = false) and add this empty ArrayList to one of the persisted
"bags" - see below.
2) when the button click handler fires, load the Array list back in from its
bag, add the new record to it, then save it back to its bag.
3) when the page is loaded after a postback (IsPostBack = true) you get the
ArrayList from the bag and assign it & DataBind to the datagrid.

Note that the ViewState bag works OK if only working on one page in your
application. If you want this data to be subsequently available to other
pages in your application, you can use the Session bag instead, which is
accessed in the same manner.

[Serializable]
protected 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; }
}
// the rest ommitted to save RSI!
}

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

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

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

addresses = (ArrayList) ViewState["Addresses"];
// note the above would usually be checked for null at this point
// or put in a try ... catch block. for now we let ASP provide
// an error page if anything is amiss.

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
// etc

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

DataGrid1.DataSource = addresses;
DataGrid1.DataBind();

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
// etc
}

Note from the above code that I have moved addresses into a local variable
that is retrieved from the bag wherever it is used. I find this helps avoid
confusion over the persistance of instance fields between method calls.

Hope this helps,
Chris.
"Stephen" wrote:
I have the following code working in order to create an array list and
populate a datagrid however everytime i click my button the first item in the
array and the first row in the datagrid are overwritten. I think this is some
sort of post back problem but i can't seem to figure out how to solve it. Can
someone help me with this please.

private ArrayList addresses;

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

if(!(ViewState["Addresses"]==null))
{
this.addresses = (ArrayList)ViewState["Addresses"];
}
else
{
this.addresses = new ArrayList();
}

}

private void Button1_Click(object sender, System.EventArgs e)
{

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
newAddress.Address3 = this.TextBox3.Text.Trim();
newAddress.Address4 = this.TextBox4.Text.Trim();
newAddress.Address5 = this.TextBox5.Text.Trim();
newAddress.Address6 = this.TextBox6.Text.Trim();

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

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

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
this.TextBox4.Text = "";
this.TextBox5.Text = "";
this.TextBox6.Text = "";
this.TextBox1.Text = "";
}

Nov 16 '05 #2
Thank you very much!!!!!! I've been trying to solve this problem and get
things working for over a day now and your code has solved it for me. Thats
brilliant. I've a lot more stuff to do with this but im on my way now. Thank
you.

I was wondering if you could help me with a small problem. The datagrid
which comes back returns with 6 columns in it. It has been requested by my
team that I try and figure out how to return the arraylist in one column. The
reason for doing this is because often the user will only fill in some of the
fields for the address and the datagrid comes back with gaps in it. Can this
be done and have you any idea how to do this. Thanks again for your help.

"Chris Ballard" wrote:
Stephen,

You are correct, it is to do with postbacks. What you need to remember is
that every time your page is loaded, your page class is recreated. This means
that any data stored in the class itself is recreated. No class data will
persist between page accesses (whether initial access or postback).

This will be the case with your addresses ArrayList.

To resolve the problem you need to:
1) create your empty ArrayList when the page is first loaded (i.e. when
IsPostBack = false) and add this empty ArrayList to one of the persisted
"bags" - see below.
2) when the button click handler fires, load the Array list back in from its
bag, add the new record to it, then save it back to its bag.
3) when the page is loaded after a postback (IsPostBack = true) you get the
ArrayList from the bag and assign it & DataBind to the datagrid.

Note that the ViewState bag works OK if only working on one page in your
application. If you want this data to be subsequently available to other
pages in your application, you can use the Session bag instead, which is
accessed in the same manner.

[Serializable]
protected 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; }
}
// the rest ommitted to save RSI!
}

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

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

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

addresses = (ArrayList) ViewState["Addresses"];
// note the above would usually be checked for null at this point
// or put in a try ... catch block. for now we let ASP provide
// an error page if anything is amiss.

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
// etc

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

DataGrid1.DataSource = addresses;
DataGrid1.DataBind();

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
// etc
}

Note from the above code that I have moved addresses into a local variable
that is retrieved from the bag wherever it is used. I find this helps avoid
confusion over the persistance of instance fields between method calls.

Hope this helps,
Chris.
"Stephen" wrote:
I have the following code working in order to create an array list and
populate a datagrid however everytime i click my button the first item in the
array and the first row in the datagrid are overwritten. I think this is some
sort of post back problem but i can't seem to figure out how to solve it. Can
someone help me with this please.

private ArrayList addresses;

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

if(!(ViewState["Addresses"]==null))
{
this.addresses = (ArrayList)ViewState["Addresses"];
}
else
{
this.addresses = new ArrayList();
}

}

private void Button1_Click(object sender, System.EventArgs e)
{

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
newAddress.Address3 = this.TextBox3.Text.Trim();
newAddress.Address4 = this.TextBox4.Text.Trim();
newAddress.Address5 = this.TextBox5.Text.Trim();
newAddress.Address6 = this.TextBox6.Text.Trim();

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

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

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
this.TextBox4.Text = "";
this.TextBox5.Text = "";
this.TextBox6.Text = "";
this.TextBox1.Text = "";
}

Nov 16 '05 #3
Stephen,

This will be quite easy to do. Remember that the columns in the data grid
relate to the public properties in your Address class. If you only expose one
public property, only one column will be included in the data grid.

Therefore you can concatenate all the private fields in the get accessor of
the property. eg:
public string FullAddress
{
get
{
return _address1 + " " + _address2; // and so on.
}
}

Chris.

"Stephen" wrote:
Thank you very much!!!!!! I've been trying to solve this problem and get
things working for over a day now and your code has solved it for me. Thats
brilliant. I've a lot more stuff to do with this but im on my way now. Thank
you.

I was wondering if you could help me with a small problem. The datagrid
which comes back returns with 6 columns in it. It has been requested by my
team that I try and figure out how to return the arraylist in one column. The
reason for doing this is because often the user will only fill in some of the
fields for the address and the datagrid comes back with gaps in it. Can this
be done and have you any idea how to do this. Thanks again for your help.

"Chris Ballard" wrote:
Stephen,

You are correct, it is to do with postbacks. What you need to remember is
that every time your page is loaded, your page class is recreated. This means
that any data stored in the class itself is recreated. No class data will
persist between page accesses (whether initial access or postback).

This will be the case with your addresses ArrayList.

To resolve the problem you need to:
1) create your empty ArrayList when the page is first loaded (i.e. when
IsPostBack = false) and add this empty ArrayList to one of the persisted
"bags" - see below.
2) when the button click handler fires, load the Array list back in from its
bag, add the new record to it, then save it back to its bag.
3) when the page is loaded after a postback (IsPostBack = true) you get the
ArrayList from the bag and assign it & DataBind to the datagrid.

Note that the ViewState bag works OK if only working on one page in your
application. If you want this data to be subsequently available to other
pages in your application, you can use the Session bag instead, which is
accessed in the same manner.

[Serializable]
protected 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; }
}
// the rest ommitted to save RSI!
}

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

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

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

addresses = (ArrayList) ViewState["Addresses"];
// note the above would usually be checked for null at this point
// or put in a try ... catch block. for now we let ASP provide
// an error page if anything is amiss.

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
// etc

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

DataGrid1.DataSource = addresses;
DataGrid1.DataBind();

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
// etc
}

Note from the above code that I have moved addresses into a local variable
that is retrieved from the bag wherever it is used. I find this helps avoid
confusion over the persistance of instance fields between method calls.

Hope this helps,
Chris.
"Stephen" wrote:
I have the following code working in order to create an array list and
populate a datagrid however everytime i click my button the first item in the
array and the first row in the datagrid are overwritten. I think this is some
sort of post back problem but i can't seem to figure out how to solve it. Can
someone help me with this please.

private ArrayList addresses;

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

if(!(ViewState["Addresses"]==null))
{
this.addresses = (ArrayList)ViewState["Addresses"];
}
else
{
this.addresses = new ArrayList();
}

}

private void Button1_Click(object sender, System.EventArgs e)
{

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
newAddress.Address3 = this.TextBox3.Text.Trim();
newAddress.Address4 = this.TextBox4.Text.Trim();
newAddress.Address5 = this.TextBox5.Text.Trim();
newAddress.Address6 = this.TextBox6.Text.Trim();

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

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

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
this.TextBox4.Text = "";
this.TextBox5.Text = "";
this.TextBox6.Text = "";
this.TextBox1.Text = "";
}

Nov 16 '05 #4
thanks very much thats brilliant

"Chris Ballard" wrote:
Stephen,

This will be quite easy to do. Remember that the columns in the data grid
relate to the public properties in your Address class. If you only expose one
public property, only one column will be included in the data grid.

Therefore you can concatenate all the private fields in the get accessor of
the property. eg:
public string FullAddress
{
get
{
return _address1 + " " + _address2; // and so on.
}
}

Chris.

"Stephen" wrote:
Thank you very much!!!!!! I've been trying to solve this problem and get
things working for over a day now and your code has solved it for me. Thats
brilliant. I've a lot more stuff to do with this but im on my way now. Thank
you.

I was wondering if you could help me with a small problem. The datagrid
which comes back returns with 6 columns in it. It has been requested by my
team that I try and figure out how to return the arraylist in one column. The
reason for doing this is because often the user will only fill in some of the
fields for the address and the datagrid comes back with gaps in it. Can this
be done and have you any idea how to do this. Thanks again for your help.

"Chris Ballard" wrote:
Stephen,

You are correct, it is to do with postbacks. What you need to remember is
that every time your page is loaded, your page class is recreated. This means
that any data stored in the class itself is recreated. No class data will
persist between page accesses (whether initial access or postback).

This will be the case with your addresses ArrayList.

To resolve the problem you need to:
1) create your empty ArrayList when the page is first loaded (i.e. when
IsPostBack = false) and add this empty ArrayList to one of the persisted
"bags" - see below.
2) when the button click handler fires, load the Array list back in from its
bag, add the new record to it, then save it back to its bag.
3) when the page is loaded after a postback (IsPostBack = true) you get the
ArrayList from the bag and assign it & DataBind to the datagrid.

Note that the ViewState bag works OK if only working on one page in your
application. If you want this data to be subsequently available to other
pages in your application, you can use the Session bag instead, which is
accessed in the same manner.

[Serializable]
protected 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; }
}
// the rest ommitted to save RSI!
}

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

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

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

addresses = (ArrayList) ViewState["Addresses"];
// note the above would usually be checked for null at this point
// or put in a try ... catch block. for now we let ASP provide
// an error page if anything is amiss.

Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
// etc

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

DataGrid1.DataSource = addresses;
DataGrid1.DataBind();

//clear down the textboxes
this.TextBox1.Text = "";
this.TextBox2.Text = "";
// etc
}

Note from the above code that I have moved addresses into a local variable
that is retrieved from the bag wherever it is used. I find this helps avoid
confusion over the persistance of instance fields between method calls.

Hope this helps,
Chris.
"Stephen" wrote:

> I have the following code working in order to create an array list and
> populate a datagrid however everytime i click my button the first item in the
> array and the first row in the datagrid are overwritten. I think this is some
> sort of post back problem but i can't seem to figure out how to solve it. Can
> someone help me with this please.
>
> private ArrayList addresses;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
>
> if(!(ViewState["Addresses"]==null))
> {
> this.addresses = (ArrayList)ViewState["Addresses"];
> }
> else
> {
> this.addresses = new ArrayList();
> }
>
> }
>
> private void Button1_Click(object sender, System.EventArgs e)
> {
>
> Address newAddress = new Address();
> newAddress.Address1 = this.TextBox1.Text.Trim();
> newAddress.Address2 = this.TextBox2.Text.Trim();
> newAddress.Address3 = this.TextBox3.Text.Trim();
> newAddress.Address4 = this.TextBox4.Text.Trim();
> newAddress.Address5 = this.TextBox5.Text.Trim();
> newAddress.Address6 = this.TextBox6.Text.Trim();
>
> this.addresses.Add(newAddress);
> //ViewState["Addresses"] = this.addresses;
>
> this.DataGrid1.DataSource = this.addresses;
> this.DataGrid1.DataBind();
>
> //clear down the textboxes
> this.TextBox1.Text = "";
> this.TextBox2.Text = "";
> this.TextBox3.Text = "";
> this.TextBox4.Text = "";
> this.TextBox5.Text = "";
> this.TextBox6.Text = "";
> this.TextBox1.Text = "";
> }

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MrMike | last post by:
I have a datagrid containing a drop-down-list. The datagrid contains Edit,Update,Cancel buttons and respective events. Whenever I make a change to one particular field (a drop-down-list in...
1
by: Joseph Luner | last post by:
I am having problem postback, (code shown below) the variable "my_str" is lost after clicking the "submit" button. Isn't it suppose to display "changed" after posting back? Is there anyway I...
1
by: Mike | last post by:
I create a dynamic control and added it to a panel. I get the selected value of the control via a btn_click() but my problem is everytime the page is postback the selected values go back to the...
1
by: Frankieboy | last post by:
I believe I've got a postback problem on our site. The strange thing is that everything works fine on my developing version, but not on the production site. I'm wondering if there may be different...
0
by: bill yeager | last post by:
I have a simple button and textbox on a webform with a RequiredFieldValidator tied to the textbox control. The problem is that a postback never occurs on the webform by clicking the button even...
0
by: phl | last post by:
hello, I have an input control which saves a file user specifies. Like this : SaveCon.PostedFile.SaveAs(sDocPath) This cause a postback which presents a problem as I want this to happen in my...
4
by: Daniel Groh | last post by:
I have two controls in my webform, BUT....one comes from a user control and the other comes from another user control, both with post back! How can i work just with one PostBack ? if(PostPack...
6
by: | last post by:
Hi all, I have a bunch of dropdownlists that are populated in client-side javascript. When i do a postback I get the following error:- Invalid postback or callback argument. Event...
9
by: Alper OZGUR | last post by:
Hi; In my code i'm checking some conditions and if the condition fails i want to postback the page. Tried using the below methods but the when form is loaded the postback status is false.. how can...
2
by: VSingh | last post by:
I have a form in a C# ASP Dotnet application. There is code in both the !Postback and Postback section. The code in Postback section always executes on the development box but when I deploy the app...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.