C# in ASP.Net - Passing complex variables

This is a discussion on C# in ASP.Net - Passing complex variables within the C# Programming forums, part of the General Programming Boards category; I'm developing an internal application that will query a database. One page grabs rows of information and displays them in ...

C# in ASP.Net - Passing complex variables

I'm developing an internal application that will query a database. One page grabs rows of information and displays them in one table. Each row has an assigned checkbox, and there is a delete button below the table. Each checked row should be deleted when the button is pressed. I've stored the checkbox IDs in an ArrayList.

The ArrayList is created in function A (the query function). When the delete button is pressed, it totally clears and recreates the form and class to load function B (the delete function).

The problem: This clears my ArrayList. Nothing will ever get deleted because the ArrayList will always be empty in function B.

How do I pass an ArrayList between functions? -OR- If it's not possible, how would I get this information from function A to function B?

I'm just guessing here but from the way you describe the problem this is how I see it.

You have rows, each row on the page has a delete button, when clicking this button the page is being refreshed, sending along some param (the rowID) with the request in one way or the other.

There are several approaches to this.

1) You cache the arrayList when its retrieved from db. You then click a delete button, sending the rowID along as parameter in the request for the page. On formload you do a check to see if this parameter is included, if so jump to the delete function , get the rowID.

Now you can do one or two things:
1.1) Keep all the deleted rowIds cached as well , and as soon as you are done deleting , submit the form again, check which rowId's have to be deleted, and delete them from the db. (this will cause only one query to be executed )
Also keep in mind that the display function to display the rows should be adjusted, only show the rows that are not in the deletedRowId list.

1.2) Each time the delete function is being called , delete the row in the db using the rowID (this will increase database connectivity, since for each row you delete you'll be using a query). Also keep in mind to delete the row from the cached arrayList (else you'll see for instance row1 appearing while its already deleted from the db).

I can think of many more ways how to tackle this problem... Just remember that database connectivity is expensive as in, so I think you are better of using an approach like 1.1

Also caching is a bit of a wrong term here, I'm talking about sessions, you can dump any kind of object into the Session in asp.net so that means even those dirty ArrayLists...

Another option I'd like to share with you although this is a very messy approach (as in do not use this):

On page load read db, click delete button, reload form, on formload check the delete param for the rowID. Delete the row from db, retrieve all rows again from db. Pump the rows into the form, display form. (this is very expensive towards databaseconnectivity, which is why I do not recommend it at all).

I was looking for session variables. Needless to say I've found them =]

Now I've come across what seems to be a catch 22:

The function queries the database and can return any number of rows. For each row, I need a "Delete" button that triggers another fuction. Here's where the problem is. I can only add an eventhandler to Button.Click in OnInit or OnLoad, which occurs before I know how many buttons I need.

Right now I've got a static amount being generated beforehand, on every single pageload. It's a dirty hack and I'd really like to know how I can create these dynamically.