The problem is described in the title. The Code is as follows:
Page code
<asp:GridView ID="gdvCustomers" runat="server" Height="210px" OnRowEditing="gdvCustomers_RowEditing" Width="586px" OnRowDeleting="gdvCustomers_RowDeleting" OnRowUpdating="gdvCustomers_RowUpdating" OnRowCancelingEdit="gdvCustomers_RowCancelingEdit" AutoGenerateColumns="False" DataKeyNames="customerID"> <Columns> <asp:BoundField DataField="customerID" HeaderText="customerID"> </asp:BoundField> <asp:BoundField DataField="companyName" HeaderText="companyName"> </asp:BoundField> <asp:BoundField DataField="address" HeaderText="address"> </asp:BoundField> <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" /> </Columns> </asp:GridView>
Background code
Protected void gdvcustomers_rowupdating (Object sender, gridviewupdateeventargs e) {string id = (textbox) This. gdvcustomers. rows [E. rowindex]. cells [0]. controls [0]). text. tostring (); string name = (textbox) This. gdvcustomers. rows [E. rowindex]. cells [1]. controls [0]). text. tostring (); string address = (textbox) This. gdvcustomers. rows [E. rowindex]. cells [2]. controls [0]). text. tostring (); string SQL = "Update MERs set companyName = '" + name + "', address = '"+ address +" 'where mermerid =' "+ ID +" '"; sqlconnection con = new sqlconnection (" Integrated Security = true; server = .; database = northwind; "); sqlcommand cmd = new sqlcommand (SQL, con); con. open (); cmd. executenonquery (); con. close (); gdvcustomers. editindex =-1; // cancel the editing status dataload ();}
Check the gdvcustomers_rowupdating event of the gridview. The code in the event is correct. After tracking the SQL statement, it is found that the read value is not the modified value, but the original value.
Finally, we found that the key to the problem was not in gdvcustomers_rowupdating, but in page_load. If we didn't determine whether it was a callback, we put the code bound to the gridview data into the ispostback judgment statement, and the problem was solved. The Code is as follows:
if (!this.IsPostBack){ DataLoad();}
I haven't gotten a webform for a long time, so I have forgotten such basic knowledge!