Error: ispostback in ASP. NET, programming with DataGrid

Source: Internet
Author: User

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
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.