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