Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
Calling DataBind on a webgrid more than once (not possible?)

I have a grid containing say a Customer List and on the same page some controls and a button (Button3).

In the click event handler of Button3, I save a new entry to the database. As such I would need to refresh the grid to reflect that new entry.

The problem is the click event handler is fired after the InitiliazeDataSource event of the webgrid is called.

In the grid's InitiliazeDataSource I simply assign the grid datasource with a dataset (no call to DataBind() is made).

To reflect the newly added entry I tried to reassign the DataSource value with a more updated/latest dataset but it doesn't reflect on the browser. I called DataBind() expicitly and it worked. The only problem is my OnItemCommand event handler for the webgrid no longer triggers right after. I have an aspButton (say ButtonEdit) inside a template column. Whenever an explicit call to DataBind is made clicking ButtonEdit causes a callback but doesn't trigger the OnItemCommand event handler. Since the webgrid has been re-initialized then, succeeding clicks on ButtonEdit will work normally until Button3 is clicked again.

So if I'm not mistaken, calling Grid.DataBind() explicitly in code seems to messes up a lot of things and event handling seems to be one of them. Can someone confirm this? or any work-around? I think this is a common scenario though and calling DataBind over and over again within a postback/callback should in my opinion not cause any issues.

One workaround I could think of is find a way to ensure that my "save" processing comes before InitializeDataSource fires. Ideally it should be on my button click handler which comes unfortunately comes after the InitializeDataSource. This would prevent me from having to fill the dataset more than once nevertheless I think it would be worth looking into.

Thanks in advance for any comment. 

Parents Reply Children