views:

77

answers:

3

Hi

I have dragged a empty asp.net table onto my webform. I generate all the rows in the code behind those.

Now my table gets filled up and has dropdown lists. When the user hits save I go through all the rows and update the values from the dropdownlist in the db.

This works all great. However if 2 columns have each have "Present" then those 2 columns should be not be shown anymore and 2 new columns get put in its place with other dropdown lists.

This all works. However you have to refresh the entire page to for the 2 columns that should go away to go away.

So what I tried to do is at the end of the button click event. Clear the whole table and then regenerate it. However when I do this then my values are not saved to the database anymore for whatever reason.

if (IsPostBack == false)
{
    // check if dummy variables exist in db- If true just generate tables with values in db. If not generate them. 

}
else
{
   // grab the values from the database
   // generate tables with the values
}

btn click event
{
   go through all rows in table(foreach loop)
   update each column in the database with cells in each row. while in foreach loop.
   //done
}

So this is how it goes and it works expect(all correct values are saved) the table is just not updated to the user.

Does not work

    if (IsPostBack == false)
    {
        // same code as above
    }

   // if postback is true do nothing. By the time it gets to the click event it says there is zero rows in the table so nothing happens.
    btn click event
    {
       // same code
    }

Fails also.

if (IsPostBack == false)
{
   // same code as above
}
else
{
   // same code as above but moved into its own method.
   gernerateTable();
}

btn click event
{
   // update all rows
   // once done clear the Tables rows
   // call generateTable()
}

This last one does nothing as for some reason it does not update anything. I don't understand why.

So what am I doing wrong with this life cycle something in my process is wrong. The code works just not when I want the table to be updated right away.

A: 

If you add controls dynamically to the page, you need to rebuild them before you can do anything with them.

What I usually do to completely bypass these types of issues is to use a repeater. You can then do your foreach loop still on the RepeaterItems. It's clean and it's relatively simple as well. No page lifecycle issues.

James Thomas
Naw I need it to look a certain way(it has too look like an attendance sheet). I tried gridview but since I had to make the whole thing editable and I was not using any of the events I decided to just not use it.
chobo2
A: 

Try moving the creation of dynamic controls to Page Init event.

SoftwareGeek
Naw get the same thing.
chobo2
+1  A: 

In building a page, ASP.NET generates control ID's as the controls are created. These ID's are rendered to the HTML and submitted by the browser back to the server. During postback, ASP.NET re-generates the controls on the page to determine ID's, then assigns data values to the controls from the Request data.

So, if you dynamically generate controls (table rows with drop-down lists) then you need to generate all those rows before you inspect, save, or modify them. A common pattern that I have used in the past is something like this:

OnPageLoad() {
  get data;
  build controls;
  bind data; 
}

OnButtonClick() {
  save data();
  build controls; // if changed by 'remove' or 'add'
  bind data;
}
jwadsack
Sorry I am not sure if I am following. First when you say get data; you mean the data in the database? Then you build the controls(ie the table and rows) with the "getdata"(from the db). Then you save the data once at the click event. I then what happens you build the controls using what data?
chobo2
Yes, 'get data' means from the database. When you save the data and build the controls then you either need to cache that data in local variables to build from or get it back from the database (which might be a good idea if it can be updated from another user or another part of the site).
jwadsack