Alas, now I found out how important the ispostback attribute is in ASP. NET ......
In page_load, check whether the page is loaded for the first time or whether the page is submitted by the user (PostBack)
If (! Ispostback ){
// Do something
}
This problem is not noticed when you access and update the database using the DataGrid, and various strange problems may occur, such as mine.
Problem description:
Use the DataGrid to access and update the database (SQL Server -- northwind -- table name: categories -- Query: Select categoryid, categoryname, description form categories), in addition to the update operation, other functions are OK. perform the following operations on the DataGrid: click "edit", the row data becomes editable, And the edit button is replaced by "Update" and "cancel. Edit the data. For example, change AAA in description to BBB. Click "Update. The intention is to use this method to replace the modified data (BBB) with the original AAA in the database. Of course, the update method is used. However, after clicking "Update", the data has not changed. I have tested it and the update method is effective. That is to say, the update method updates not the new data, but the old data before the modification, the data is not updated (actually updated ). Note that my page_load event is as follows:
Private void page_load (Object sender, system. eventargs E)
{
// Place the user hereCodeTo initialize the page
Olead. Fill (DS); // olead -- oledbadapter
DG. databind (); // DG -- DataGrid
}
Analyze the problem (correct your suggestion ):
Because ispostback is not used to determine whether the page is loaded for the first time, data binding of the DG (DataGrid) will be performed no matter what circumstances, as long as there is a PostBack. Therefore, after any sumbit operation, the DG will bind data to the database and ignore the data on the page.
After the selected data is modified, When you click "Update", submit the data modified on this page, and the page_load event will immediately occur, without processing the modified data, the server becomes the original page (How does Ms speed up ?), If you find that DG. databind () is executed, the database update is abandoned, so you cannot see the update result.
Solution:
The solution is simple. You can add the ispostback judgment to the page_load event.
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Olead. Fill (DS); // olead -- oledbadapter
If (! Ispostback)
{
DG. databind (); // DG -- DataGrid
}
}