views:

847

answers:

2

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

But I’ve noticed that if I, for whatever reason, also manually call 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.

Could someone explain why updates don’t happen?

+5  A: 

It's because the Page_Load is fired BEFORE the SqlDataSource.Updating and SqlDataSource.Updated events fire. This means that the GridView resets to what it was before the user edits.

Please review the Page Lifecycle documentation for details. The SqlDataSource.Updating and SqlDataSource.Updated events happen in the Postback Event Handling section.

David Stratton
I'm familiar with Page lifecycle, but I wasn't aware that DataBind() resets all GridView's values. Much appreciated
SourceC
A: 

I have the same problem but with a DropDownList, I don't think it is connected to the Page_Load, I have the code in the page_load surrounded with 'if (!IsPostBack)' and I checked also with the debuger to see that it is NOT been called...

It seems that what ever I do the DataBind doesn't work: I tried to call both the SqlDataSource.DataBind an the DropDownList.DataBind --- NOTHING.

I tired to set the SqlDataSource to no caching but it cause it to not work at all....

Is this a .NET bug???

Is there a way to by-pass it (with out writing the data binding my self) ???

Thanks Elia

elia
You should really write a new question for this - you've much more chance of getting an answer, this isn't really a discussion board in that fashion. A code snippet would also help when you ask it.
Paddy