iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
This is a follow-up to the following thread:
http://www.intersoftpt.com/Community/WebGrid/Simple-Edit-screen-with-WebGrid/
The problem there was solved. Because this is a new problem, I started a new thread. Attached is a simplified sample, written from scratch and showing the problem.
What happens:
1. Start the application and change the URL to include a parameter ?id=1 and set breakpoints
2. Event Page_Init
3a Event Page_Load
3b LoadDataFromDatabase
4. grid_InitializeDataSource
5. Page_PreRender
6. n x grid_InitializeRow
7. Now manually change on the screen some selections in the grid and press Save
8. Event Page_Init
9a Event Page_Load (PostBack is true)
9b LoadDataFromDatabase
10. grid_InitializeDataSource
11a btnSave_Click
11b ReadDataFromUserSelection
12. Page_PreRender
13. n x grid_InitializeRow
Comment 1: Step 9b (LoadDataFromDatabase) is also necessary even if PostBack is true, because we need to re-read all the read-only data. Also we need this to create our array of data in the internal structures. Otherwise we would have to store all this in the session or viewstate somehow. The data will get updated later in step 11b to hold the real values.
Comment 2: Well, in step 11 we don't write the data to the database as we should do, because for this example the DB data is hardcoded. But as you can see, no additional call to read the database is being done. In step 11b the data in the internal structure (_Models) is correct and reflects the users changes. You can see that no additional call to ReadDataFromDatabase is being made, so it should display the last selection.
Problem: After saving (after all above steps) the screen shows the initial database data instead of the changed data. At some point the user changes are reverted for some reason.
.NET Framework 2.0, ISNet Framework 3.0.5000.705, WebGrid 6.0.7200.218, testing on Windows XP IIS
I did not receive any answer the last four days searching for a result. In the meantime a found a solution myself. I don't know if this is the best design, but it works. For all others having the same problem, I'll show my solution here.
Looking through the samples I saw that in Page_Load the RootTable of the control is always empty. The sample just refills the the RootTable again, but the screen display remains on the old values. I don't fully understand this behaviour, or the reasons for this, but with this knowledge I could write a workaround.
My changes to my given sample in detail:
- In Page_Load remove the call to update the data from the user selections. (This will be done later.)
- In btnSave_Click add this call to update the data from the user selections.
- In Page_PreRender loop through all rows in RootTable and set row.Checked depending on the value in the column "Incl". Do this only if not in PostBack mode.
- In grid_InitializeRow remove the row.Checked=... statement as we moved this to Page_PreRender.
- In grid_initializeRow read the Default radio button selection by reading Request.Form("radioGroupDefaultModel"). This can only be read in case of a Postback.
- In grid_InitializeRow set the CHECKED status of the row depending on two criteria: a) If the value could be read (previous point) then compare the ID and set the value to CHECKED if it is this one b) This is for the initial load (no postback): Set the CHECKED status depending on the value of the column in the row.
Some comments:
We have two special things in this sample. One is that we use checkboxes to display a column. The WebGrid supports checkboxes, but those were not intended to display data. Because we only use one such column, we can use this feature. The problem is that in InitializeRow we just copied the value of the checkbox column to the checked status of the column. When we came into the postback, the selection was not preserved, because it was overwritten by this assignment. To fix this the assignment of the checked status is done in Page_PreRender, and only if not in PostBack mode.
The second special thing is that we have a database column Is_Default_Model. This will actually get rendered to a radio box button, so only one row can be selected. The rendering of the radiobox HTML in InitializeRow is ok. But we have to set the CHECKED value different. Previously we just set this status by looking at the database column. Now we do this only if not in PostBack mode, so only the first time the page loads. In InitializeRow we cannot access the PostBack flag, but we access the Request.Form("radioGroupDefaultModel") where we see what the user selected. If we have this value, we also know which radio button needs to be checked. If not, we're not in postback mode and can copy the checked status from the database row.
In Page_Load we cannot read the user selections, because the RootTable is empty - grid_InitializeDataSource was not called yet. So move this to the button click event. We don't need the data earlier anyway.
Let me know if you see anything to improve in this design.
I just found the problem writing this thread. It looks like the step 10 comes between 9b and 11b, so the refresh of the data is at the wrong place. That's why after the postback the wrong data gets shown and in the internal structures it is correct.
This means that the design is wrong. How would it be correct, or what should I change here?
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname