473,387 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 = "";
}
Jul 21 '05 #1
2 1163
Hi Stephen

Add a check whether this is a postback or not, and then initialize the
addresses variable accordingly.

Eg:
If(!IsPostBack)
{
// this is not postback, it's is the initial run, so do the first intialize
}

HTH,
Rakesh Rajan

"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 = "";
}

Jul 21 '05 #2
I added in this code on the button 1 click as indicated below, however now
when I click submit the page doesn't do anything. i.e. the datagrid doesn't
come back at all now. Do you know where im going wrong? and where I can
insert the code to get it to work properly. Some suggested uncommenting the
line: -
//ViewState["Addresses"] = this.addresses;
However when I do this I get the following error: -
The type 'WebApplication2.Address' must be marked as Serializable or have a
TypeConverter other than ReferenceConverter to be put in viewstate.
Is this anything to do with it??
Thanks for your help so far

if(!IsPostBack)
{
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 = "";
}
"Rakesh Rajan" wrote:
Hi Stephen

Add a check whether this is a postback or not, and then initialize the
addresses variable accordingly.

Eg:
If(!IsPostBack)
{
// this is not postback, it's is the initial run, so do the first intialize
}

HTH,
Rakesh Rajan

"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 = "";
}

Jul 21 '05 #3

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

Similar topics

0
by: Xavier Osa | last post by:
Hi, I have an ASP.Net web page that you can download a file. As Fergunson's problem, it prompts twice dialog boxes only if I select Open button. If I select Save button, it prompts once. I'm...
10
by: Krista Lemieux | last post by:
I'm new to ASP.NET and I'm not use to the way things are handled with this technology. I've been told that when I have a control, I should only bind the data to it once, and not on each post back...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
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...
1
by: Timbo | last post by:
Hi all, This is my first message here so i'll try and include all the information that will help you help me out, if possible. Basically I am using C# in ASP.NET 2.0 and have a Repeater...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.