R – Why does GridView reset its values to what it was before user edits

asp.netdata-bindinggridview

If I bind GridView (via DataSourceID attribute) to SqlDataSource and set SelectCommand and UpdateCommand attributes, then everything works perfectly.

But if we manually call GridView.DataBind inside Page_Load(), then SqlDataSource doesn’t perform any updates, even though SqlDataSource.Updating and SqlDataSource.Updated events do fire when GridView’s Update button is clicked. I think this is due to the fact that GridView resets to what it was before the user edits:

a) Why does GridView reset its values if we manually call DataBind() inside Page_Load()?

b) Since Update operation doesn’t work when manually calling DataBind, I would then assume that Delete operation also wouldn’t work, but it does. Why?

cheers

Best Answer

I believe Page_Load runs before your changes take place, thus you bind the old data before you run the updates

Wrap the bind in a If Not IsPostBack when under Page_Load, i believe that will fix your problem.