Thread: C# in ASP.Net - Passing complex variables

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    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?

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    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).
    Last edited by GanglyLamb; 01-17-2008 at 03:23 AM.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    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.

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Some code i dug up from a project I did a year ago:

    Code:
        private void VullBestellingTabel(ArrayList bestelling) {
            
            this.bestellingHeader_lbl.Visible = true;
            
            this.bestelling_tbl.Rows.Clear();
            double totaalPrijs = 0;
            TableRow r;
            TableCell c;
            for (int i=0;i<bestelling.Count;i++) {
                //hr toevoegen
                if (i == 0) {
                    r = new TableRow();
                    c = new TableCell();
                    c.ColumnSpan = 4;
                    c.CssClass = "bestellingHeader";
                    c.Text = "&nbsp;";
                    r.Cells.Add(c);
                    this.bestelling_tbl.Rows.Add(r);
                }
    
                Pizza p = (Pizza) bestelling[i];
    
                totaalPrijs += p.Prijs;
                //naam invullen
                r = new TableRow();
                c = new TableCell();
    
                LinkButton rem = new LinkButton();
                rem.ID = i.ToString();
    
                rem.Click += new EventHandler(rem_Click);
                rem.Text = "Verwijder pizza:";
                c.Controls.Add(rem);
                c.Wrap = false;
                c.VerticalAlign = VerticalAlign.Bottom;
                r.Cells.Add(c);
    
                c = new TableCell();
                c.Text = "&nbsp;€ " + p.Prijs;
                c.Wrap = false;
                c.VerticalAlign = VerticalAlign.Bottom;
                r.Cells.Add(c);
    
                c = new TableCell();
                c.Text = " - " + p.Pizzanaam;
                c.Wrap = false;
                c.VerticalAlign = VerticalAlign.Bottom;
                r.Cells.Add(c);
    
                c = new TableCell();
                c.Text = " - topppings: " + p.Toppings;
                c.Wrap = false;
                c.VerticalAlign = VerticalAlign.Bottom;
                r.Cells.Add(c);
    
                this.bestelling_tbl.Rows.Add(r);
    
                r = new TableRow();
                c = new TableCell();
    
                //Ruimte tussenlaten
                c.Text = "&nbsp;";
                r.Cells.Add(c);
                this.bestelling_tbl.Rows.Add(r);
            }
            if (bestelling.Count != 0) {
                c = new TableCell();
                r = new TableRow();
                c.CssClass = "bestellingHeader";
                c.Text = "Totaal:";
                c.Font.Bold = true;
    
                r.Cells.Add(c);
    
                c = new TableCell();
                c.Text = "&nbsp;€ " + totaalPrijs.ToString();
                c.CssClass = "bestellingHeader";
                c.ColumnSpan = 3;
                c.Font.Bold = true;
                r.Cells.Add(c);
    
                this.bestelling_tbl.Rows.Add(r);
            }
            this.send_mail_link.Visible = true;
        }
    I know it looks like a mess but anyhow: its a order page where the order is stored in a session var (passed by the variable called bestelling to the function).

    I dynamically build the table, with the rows and cells, then add a Linkbutton (this can also be a normal button i guess).

    Code:
                LinkButton rem = new LinkButton();
                rem.ID = i.ToString();
    
                rem.Click += new EventHandler(rem_Click);
                rem.Text = "Verwijder pizza:";
                c.Controls.Add(rem);
    This attaches an event to the linkbutton, for the event itself, to know which button was pressed I use the following code:

    Code:
        /// <summary>
        /// De event die uitgevoerd wordt als er op een rem link geklikt wordt 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rem_Click(object sender, EventArgs e) {
            LinkButton s = (LinkButton)sender;
            this.RemovePizza(Int32.Parse(s.ID));
            this.BevolkToppingPrijsLijst();
        }
    So to bring this back to what you want to do: create a button for each row, set a property of the linkbutton (I used ID) according to the rowID.

    Now in the eventhandler check for the sender object, get the ID from that object and you know which row to delete.

    Note that there might be easier ways to build this table, by using DataSets etc... (I'm just giving you one way to do this, since every other way is kind of similar to this one).
    Last edited by GanglyLamb; 01-19-2008 at 02:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing multiple variables using function
    By m3rk in forum C++ Programming
    Replies: 12
    Last Post: 01-24-2009, 03:11 PM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Passing variables to system() function?
    By carter192 in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 09:44 PM
  4. Passing Variables To C++ Through HTML
    By retretret in forum C++ Programming
    Replies: 8
    Last Post: 01-17-2002, 08:42 AM
  5. passing variables
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2001, 02:41 PM