ViewState problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an arraylist which takes in values and im trying to add values to the
array list everytime the button is clicked. Im trying to use the viewstate
but seem to be getting the following build error.

'System.Web.UI.Control.ViewState' denotes a 'property' where a 'method' was
expected

Can someone please tell me what I have done wrong. Here is the code which I
have added to the web form below.

Thanks for your help


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();
}
 
You are trying to use paren to access the Address value from viewstate
instead of square brackets. So you are using syntax for calling a method
'ViewState' instead of accessing the indexed ViewState property.
 
Back
Top