Hi,
I have a little problem with ArrayList that make me crazy,
I have a usual Web Application that contains two Buttons,
When i click on Button1 it would add an object to MyArrayList and Button2 Shows the Objects.
but MyArrayList.Count is Zero allways.
here is my code:
public partial class DoFeed : System.Web.UI.Page
{
private ArrayList TheItems = new ArrayList();
protected void Page_Load(object sender, EventArgs e){ }
protected void Button1_Click(object sender, EventArgs e)
{
OneItem Item = new OneItem();
Item.Title = TitleTXT.Text;
Item.Link = LinkTXT.Text;
TheItems.Add(Item);
}
protected void Button2_Click(object sender, EventArgs e)
{
int m = TheItems.Count; //it is zero allways
for(int i=0; i<TheItems.Count i++)
.......
}
}
Problem with ArrayList
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mohammed Sajid15 pointsPosted Feb 14, 2006, 5:38 AM EST
Hi,
Do the modifications as below and it should work.
public partial class DoFeed : System.Web.UI.Page
{
private ArrayList TheItems = new ArrayList();
// Declaration should go here......
OneItem Item;
protected void Page_Load(object sender, EventArgs e){ }
protected void Button1_Click(object sender, EventArgs e)
{
//OneItem Item = new OneItem();
// Instantiate here....
Item = new OneItem();
Item.Title = TitleTXT.Text;
Item.Link = LinkTXT.Text;
TheItems.Add(Item);
}
protected void Button2_Click(object sender, EventArgs e)
{
int m = TheItems.Count; //it is zero allways
for(int i=0; i<TheItems.Count i++)
.......
}
}
Regards,
Mohammed Sajid
Thang Truong Nguyen Quoc2 pointsPosted Feb 14, 2006, 5:39 PM EST
Regards,
ThangTruong
kourosh saleh3 pointsPosted Feb 14, 2006, 9:26 PM EST
I have tested it but it doesn't work,
the problem is about "state" when you make a event in a web page all controls were recreated and you must save the state, by session or ViewState,
I have solve my problem thanks,
I am not good to explain,
Anwar Buchoo89 pointsPosted Feb 21, 2006, 8:18 PM EST
But, it's not always advisable to save data into Session or ViewState object as this can slow down the loading of the web page and consume memory for a amount of time.
Think, instead, of using a hidden field within your web form (.aspx) to save the data.
Rgds.
NB: Just for information, the data of variables are lost when the form is posted back.