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 ex开发者_如何学编程pect(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.
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;
}
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.
Try moving the creation of dynamic controls to Page Init event.
精彩评论