views:

212

answers:

3

Hi there I'm Working on an Asp.net/C# application. I want to bind a list of objects to a grid view. I've been looking a while to the ObjectDataSource control and it loads data to my datagrid as soon as my page loads. Can I change this behaviour? For example, how can I set DataBinding only when a button on my page gets clicked?

tks

A: 

You can create the objectdatasource in the code behind and do the bind onclick.

Wil
Bad idea, because you might loose the benefits that you get by using the ODS.
citronas
+1  A: 

You can hook into the ObjectDataSource's Selecting event and cancel any selects that don't match the criteria you're looking for. I do this all the time based on values in other controls.

Look at the e.Cancel property for that event.

Barry
+1  A: 

Do not set the datasource in markup and simply do it in the code-behind on the OnClick event of the button.

Programatically, you can specify the object to bind to as shown in the answer to this question...

http://stackoverflow.com/questions/398231/asp-net-2-0-specifying-an-instance-of-an-object-for-an-objectdatasource/398261

George
This method works for me as well. I described it in my answer to this question: http://stackoverflow.com/questions/831208/databind-and-postback/3015512#3015512
PeterTheNiceGuy