views:

290

answers:

3

Hi

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

+1  A: 

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.

TheDPQ
That's not what I'm asking.I know that in order for Update operation to succeed, then on postback we shouldn't manually call DataBind.
SourceC
A: 

You will need to assign the GridView with source and also databind it. Something along the lines of the following code:

Page_Load 

if(!Page.IsPostBack) 
{
    gv1.DataSource = GetData(); 
    gv1.DataBind(); 
}
azamsharp
A: 

Yes thanks it works as you specified.

pradeep