Arraylist issue

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Hi all,

I have...

using System.Collections;
namespace MyApp.Templates
{
public class MyPage : System.Web.UI.Page
{
ArrayList MyArray = new ArrayList();
int MyPointer;

private void FillArray_Click(object sender,
EventArgs e)
{
foreach (DataGridItem i in MyGrid.Items)
{
CheckBox cbSelectRows = (CheckBox)
i.FindControl("cbSelect");
if (cbSelectRows.Checked)
{
MyArray.Add(((Label) i.FindControl
("DFESLabel")).Text.ToString());
Trace.Warn(((Label) i.FindControl
("DFESLabel")).Text.ToString());
}
}
MyPointer = 0;
Response.Write (MyArray[MyPointer].ToString
());
}


private void NextButton_Click(object sender,
EventArgs e)
{
Trace.Warn("ArrayCount",
MyArray.Count.ToString());
MyPointer ++;
Trace.Warn(MyPointer.ToString());
Response.Write(MyArray MyPointer].ToString());
}

}
}


Notice my arraylist is defined outside of any functions
or events. This is to make it useable in any of the
functions.

When I do FillArray_Click, the array fills up correctly.

If however, once I have filled the arraylist, then click
NextButton, the arraylist appears to be empty. How come?
I have already filled it, it is a global arraylist, so to
me, it should remain filled.

This is C# in ASP.NET.

Thanks for any help.
Regards,
Dave Colliver.
http://www.BlackpoolFOCUS.com
 
It wrote a lot of times. :))

foreach (DataGridItem i in MyGrid.Items)
{
i.Cell[CellNumber].FindControl("ControlID"); // It returns your
control
i.Cell[CellNumber].Controls[0]; // It returns your control too.
}
Any Item has cells. You have to defive a cell number. adn just then you can
do the serch of the control.

Mike
 
Hi,

In the foreach, I am adding the item value from a label
in a datagrid into the arraylist. This appears to work.
However, when I next try to read the arraylist, there is
nothing in it.

I have just tried reading direct form the grid (without a
foreach) and it sort of works...

e.g.
Trace.Warn("FromDG", ((Label) MyListGrid.Items
[MyPointer].FindControl("DFESLabel")).Text.ToString());

However, I have a column of checkboxes. I check these as
items I want to cycle through. In my original foreach, I
am reading if the checkbox is checked, then transfer the
row item into the arraylist. This gives me finer (and I
would assume faster overall) control as I don't have to
go through a DG that is 400 lines long for every press of
the next/previous buttons.

As yet, I don't know how to break out of a foreach loop
(.NET newbie).

I was hoping not to do a foreach in every button click,
just index the position of an arraylist.

Advice still appreciated.

Best regards,
Dave Colliver.
http://www.BakewellFOCUS.com

-----Original Message-----
It wrote a lot of times. :))

foreach (DataGridItem i in MyGrid.Items)
{
i.Cell[CellNumber].FindControl("ControlID"); // It returns your
control
i.Cell[CellNumber].Controls[0]; // It returns your control too.
}
Any Item has cells. You have to defive a cell number. adn just then you can
do the serch of the control.

Mike

Hi all,

I have...

using System.Collections;
namespace MyApp.Templates
{
public class MyPage : System.Web.UI.Page
{
ArrayList MyArray = new ArrayList();
int MyPointer;

private void FillArray_Click(object sender,
EventArgs e)
{
foreach (DataGridItem i in MyGrid.Items)
{
CheckBox cbSelectRows = (CheckBox)
i.FindControl("cbSelect");
if (cbSelectRows.Checked)
{
MyArray.Add(((Label) i.FindControl
("DFESLabel")).Text.ToString());
Trace.Warn(((Label) i.FindControl
("DFESLabel")).Text.ToString());
}
}
MyPointer = 0;
Response.Write (MyArray[MyPointer].ToString
());
}


private void NextButton_Click(object sender,
EventArgs e)
{
Trace.Warn("ArrayCount",
MyArray.Count.ToString());
MyPointer ++;
Trace.Warn(MyPointer.ToString());
Response.Write(MyArray MyPointer].ToString ());
}

}
}


Notice my arraylist is defined outside of any functions
or events. This is to make it useable in any of the
functions.

When I do FillArray_Click, the array fills up correctly.

If however, once I have filled the arraylist, then click
NextButton, the arraylist appears to be empty. How come?
I have already filled it, it is a global arraylist, so to
me, it should remain filled.

This is C# in ASP.NET.

Thanks for any help.
Regards,
Dave Colliver.
http://www.BlackpoolFOCUS.com


.
 
Hi,

When you click the "Next" button, the post back occurs, and hence the
arraylist is again filled with empty. Check for PostBack event of Page, and
fill the arraylist accordingly.

Prakash.C
 
Hi,

I thought the idea of making the arraylist global would
negate the issue of postback and make it survivable. I am
not filling the arraylist on the page_load, I am using an
onclick event to fill the arraylist, then another onclick
to index the position and read the arraylist.

How else can I make the arraylist survivable?

Thanks.
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
-----Original Message-----
Hi,

When you click the "Next" button, the post back occurs, and hence the
arraylist is again filled with empty. Check for PostBack event of Page, and
fill the arraylist accordingly.

Prakash.C

Dave said:
Hi all,

I have...

using System.Collections;
namespace MyApp.Templates
{
public class MyPage : System.Web.UI.Page
{
ArrayList MyArray = new ArrayList();
int MyPointer;

private void FillArray_Click(object sender,
EventArgs e)
{
foreach (DataGridItem i in MyGrid.Items)
{
CheckBox cbSelectRows = (CheckBox)
i.FindControl("cbSelect");
if (cbSelectRows.Checked)
{
MyArray.Add(((Label) i.FindControl
("DFESLabel")).Text.ToString());
Trace.Warn(((Label) i.FindControl
("DFESLabel")).Text.ToString());
}
}
MyPointer = 0;
Response.Write (MyArray[MyPointer].ToString
());
}


private void NextButton_Click(object sender,
EventArgs e)
{
Trace.Warn("ArrayCount",
MyArray.Count.ToString());
MyPointer ++;
Trace.Warn(MyPointer.ToString());
Response.Write(MyArray MyPointer].ToString ());
}

}
}


Notice my arraylist is defined outside of any functions
or events. This is to make it useable in any of the
functions.

When I do FillArray_Click, the array fills up correctly.

If however, once I have filled the arraylist, then click
NextButton, the arraylist appears to be empty. How come?
I have already filled it, it is a global arraylist, so to
me, it should remain filled.

This is C# in ASP.NET.

Thanks for any help.
Regards,
Dave Colliver.
http://www.BlackpoolFOCUS.com
.
 
Hi Dave,


As you know the HTTP protocol is stateless protocol, so with each request we
need to build all required objects and populate variable from scratch. Even
if your variables or objects are global they will not be available in next
postback request.

To pass this data from one request to next request we need to store it
somewhere. There are few possible ways to do it. Depending on your
requirements you can use the most appropriate method.

1. Use Session to store your data.
2. Use Cache to store in application cache.
3. Make it a static variable of global application object.
4. Serialize the data and store it either in viewstate or file system, and
then deserialize in next request.

Hope it will help.

--
Cheers,
Rahul Anand

Dave said:
Hi,

I thought the idea of making the arraylist global would
negate the issue of postback and make it survivable. I am
not filling the arraylist on the page_load, I am using an
onclick event to fill the arraylist, then another onclick
to index the position and read the arraylist.

How else can I make the arraylist survivable?

Thanks.
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
-----Original Message-----
Hi,

When you click the "Next" button, the post back occurs, and hence the
arraylist is again filled with empty. Check for PostBack event of Page, and
fill the arraylist accordingly.

Prakash.C

Dave said:
Hi all,

I have...

using System.Collections;
namespace MyApp.Templates
{
public class MyPage : System.Web.UI.Page
{
ArrayList MyArray = new ArrayList();
int MyPointer;

private void FillArray_Click(object sender,
EventArgs e)
{
foreach (DataGridItem i in MyGrid.Items)
{
CheckBox cbSelectRows = (CheckBox)
i.FindControl("cbSelect");
if (cbSelectRows.Checked)
{
MyArray.Add(((Label) i.FindControl
("DFESLabel")).Text.ToString());
Trace.Warn(((Label) i.FindControl
("DFESLabel")).Text.ToString());
}
}
MyPointer = 0;
Response.Write (MyArray[MyPointer].ToString
());
}


private void NextButton_Click(object sender,
EventArgs e)
{
Trace.Warn("ArrayCount",
MyArray.Count.ToString());
MyPointer ++;
Trace.Warn(MyPointer.ToString());
Response.Write(MyArray MyPointer].ToString ());
}

}
}


Notice my arraylist is defined outside of any functions
or events. This is to make it useable in any of the
functions.

When I do FillArray_Click, the array fills up correctly.

If however, once I have filled the arraylist, then click
NextButton, the arraylist appears to be empty. How come?
I have already filled it, it is a global arraylist, so to
me, it should remain filled.

This is C# in ASP.NET.

Thanks for any help.
Regards,
Dave Colliver.
http://www.BlackpoolFOCUS.com
.
 
Hi,

Yes, I understand it is a stateless protocol (I have
extensive classic asp), but I would have thought that a
global variable would have held itself, probably using
the viewstate. Perhaps not. I thought the idea of
webforms was to make it work like winforms, in that
setting a value would hold between pages.

Perhaps I should make an invisible datagrid (or
datalist), populate it with the arraylist, then read it
back (as grids appear to hold state)

Cheers.
Dave.
http://www.NorthamptonFOCUS.com
-----Original Message-----
Hi Dave,


As you know the HTTP protocol is stateless protocol, so with each request we
need to build all required objects and populate variable from scratch. Even
if your variables or objects are global they will not be available in next
postback request.

To pass this data from one request to next request we need to store it
somewhere. There are few possible ways to do it. Depending on your
requirements you can use the most appropriate method.

1. Use Session to store your data.
2. Use Cache to store in application cache.
3. Make it a static variable of global application object.
4. Serialize the data and store it either in viewstate or file system, and
then deserialize in next request.

Hope it will help.

--
Cheers,
Rahul Anand

Dave said:
Hi,

I thought the idea of making the arraylist global would
negate the issue of postback and make it survivable. I am
not filling the arraylist on the page_load, I am using an
onclick event to fill the arraylist, then another onclick
to index the position and read the arraylist.

How else can I make the arraylist survivable?

Thanks.
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
-----Original Message-----
Hi,

When you click the "Next" button, the post back occurs, and hence the
arraylist is again filled with empty. Check for
PostBack
event of Page, and
fill the arraylist accordingly.

Prakash.C

:

Hi all,

I have...

using System.Collections;
namespace MyApp.Templates
{
public class MyPage : System.Web.UI.Page
{
ArrayList MyArray = new ArrayList();
int MyPointer;

private void FillArray_Click(object sender,
EventArgs e)
{
foreach (DataGridItem i in MyGrid.Items)
{
CheckBox cbSelectRows = (CheckBox)
i.FindControl("cbSelect");
if (cbSelectRows.Checked)
{
MyArray.Add(((Label) i.FindControl
("DFESLabel")).Text.ToString());
Trace.Warn(((Label) i.FindControl
("DFESLabel")).Text.ToString());
}
}
MyPointer = 0;
Response.Write (MyArray [MyPointer].ToString
());
}


private void NextButton_Click(object sender,
EventArgs e)
{
Trace.Warn("ArrayCount",
MyArray.Count.ToString());
MyPointer ++;
Trace.Warn(MyPointer.ToString());
Response.Write(MyArray
MyPointer].ToString
());
}

}
}


Notice my arraylist is defined outside of any functions
or events. This is to make it useable in any of the
functions.

When I do FillArray_Click, the array fills up correctly.

If however, once I have filled the arraylist, then click
NextButton, the arraylist appears to be empty. How come?
I have already filled it, it is a global arraylist,
so
to
me, it should remain filled.

This is C# in ASP.NET.

Thanks for any help.
Regards,
Dave Colliver.
http://www.BlackpoolFOCUS.com


.
.
 
Hi,

Storing it in ViewState will be a good choice. The arraylist will be sent
to client and then returned back. It will cause extra data to be sent to
client. The other options are Cache, Session and Application.

To add to view state, the command is ViewState("<variablename>") =
<arraylist>

Hope it helps

Prakash.C

Dave said:
Hi,

Yes, I understand it is a stateless protocol (I have
extensive classic asp), but I would have thought that a
global variable would have held itself, probably using
the viewstate. Perhaps not. I thought the idea of
webforms was to make it work like winforms, in that
setting a value would hold between pages.

Perhaps I should make an invisible datagrid (or
datalist), populate it with the arraylist, then read it
back (as grids appear to hold state)

Cheers.
Dave.
http://www.NorthamptonFOCUS.com
-----Original Message-----
Hi Dave,


As you know the HTTP protocol is stateless protocol, so with each request we
need to build all required objects and populate variable from scratch. Even
if your variables or objects are global they will not be available in next
postback request.

To pass this data from one request to next request we need to store it
somewhere. There are few possible ways to do it. Depending on your
requirements you can use the most appropriate method.

1. Use Session to store your data.
2. Use Cache to store in application cache.
3. Make it a static variable of global application object.
4. Serialize the data and store it either in viewstate or file system, and
then deserialize in next request.

Hope it will help.

--
Cheers,
Rahul Anand

Dave said:
Hi,

I thought the idea of making the arraylist global would
negate the issue of postback and make it survivable. I am
not filling the arraylist on the page_load, I am using an
onclick event to fill the arraylist, then another onclick
to index the position and read the arraylist.

How else can I make the arraylist survivable?

Thanks.
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com

-----Original Message-----
Hi,

When you click the "Next" button, the post back
occurs, and hence the
arraylist is again filled with empty. Check for PostBack
event of Page, and
fill the arraylist accordingly.

Prakash.C

:

Hi all,

I have...

using System.Collections;
namespace MyApp.Templates
{
public class MyPage : System.Web.UI.Page
{
ArrayList MyArray = new ArrayList();
int MyPointer;

private void FillArray_Click(object sender,
EventArgs e)
{
foreach (DataGridItem i in MyGrid.Items)
{
CheckBox cbSelectRows = (CheckBox)
i.FindControl("cbSelect");
if (cbSelectRows.Checked)
{
MyArray.Add(((Label) i.FindControl
("DFESLabel")).Text.ToString());
Trace.Warn(((Label) i.FindControl
("DFESLabel")).Text.ToString());
}
}
MyPointer = 0;
Response.Write (MyArray [MyPointer].ToString
());
}


private void NextButton_Click(object sender,
EventArgs e)
{
Trace.Warn("ArrayCount",
MyArray.Count.ToString());
MyPointer ++;
Trace.Warn(MyPointer.ToString());
Response.Write(MyArray MyPointer].ToString
());
}

}
}


Notice my arraylist is defined outside of any
functions
or events. This is to make it useable in any of the
functions.

When I do FillArray_Click, the array fills up
correctly.

If however, once I have filled the arraylist, then
click
NextButton, the arraylist appears to be empty. How
come?
I have already filled it, it is a global arraylist, so
to
me, it should remain filled.

This is C# in ASP.NET.

Thanks for any help.
Regards,
Dave Colliver.
http://www.BlackpoolFOCUS.com


.
.
 
Back
Top