Not the wrong question: IsPostBack in asp.net, programming with DataGrid
Source: Internet
Author: User
asp.net|datagrid| problem Alas, only now found that the original in asp.net IsPostBack attribute is how important ...
It is generally in Page_Load to check whether it is the first time to load this page or to determine whether the user submitted (postback)
if (! IsPostBack) {
Do something
}
When you use the DataGrid to access and update the database without noticing this problem, there are a variety of strange problems, such as mine.
Problem Description:
Accessing and updating databases with a DataGrid (SQL Server--northwind--table name:categories--query:select categoryid,categoryname,description Form categories), except for the update operation, other functions OK. Do the following for the DataGrid: Click Edit, the corresponding row data becomes editable, and the edit button is replaced by "Update" and "Cancel". Edit the data, such as changing the AAA in description to BBB. Click the "Update" button again. The intention is to use this method to replace the modified data (BBB) in the database of the original AAA, of course, using the Update method. But after the "update", the data has not changed, I have done the test, the Update method is valid, that is, the Update method is not updated with the new data, but not the old data before the modification, resulting in the data is not updated (is actually updated). Note that my Page_Load event is as follows
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Olead.fill (DS);//olead--oledbadapter
Dg. DataBind ();//dg--datagrid
}
Analysis of problems (individual conjecture, please correct me):
Because the IsPostBack is not used to determine whether this page is loaded for the first time, the DG (DataGrid) is data bound whenever there is a postback. So after any sumbit, DG would go to the database to bind the data without ignoring the data in the page.
When the selected data has been modified, in the point of "Update", submit this page modified data, and immediately encountered Page_Load event, unequal to modify the data processing, the server on the original page (MS using this method to improve speed?) ), found DG. DataBind (), executes, and then discards the database update, so the update results are not visible.
Solve the problem:
The solution is very simple, Page_Load incident with the ispostback of the judgment on it.
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Olead.fill (DS);//olead--oledbadapter
if (! IsPostBack)
{
Dg. DataBind ();//dg--datagrid
}
}
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