0

I have the following declaration in my .ASPX :

<asp:ObjectDataSource ID="ODS" runat="server" 
    SelectMethod="GetPlayersData" DataObjectTypeName="DAL.MyObject">
</asp:ObjectDataSource>

How can I place the data in the ObjectDataSource from the code behind ?

This :

var items = MyFunctions.GetPlayersData().ToList<object>();
ODS.SelectParameters = (ParameterCollection)items;

Doesn't work.

leppie
  • 115,091
  • 17
  • 196
  • 297
JAN
  • 21,236
  • 66
  • 181
  • 318
  • possible duplicate of [How do I set up ObjectDataSource select parameters at runtime](http://stackoverflow.com/questions/235166/how-do-i-set-up-objectdatasource-select-parameters-at-runtime) – Philip Pittle Nov 17 '14 at 09:37
  • According to http://stackoverflow.com/a/236420/1224069, you need to hook into the `OnSelecting` event and you can set the params there. – Philip Pittle Nov 17 '14 at 09:39
  • @ Philip Pittle: You didn't understand me , I need to put the entire list in the ObjectDataSource , the link you provided doesn't show how to put an entire list or collection in ObjectDataSource , only sets the parameters names . – JAN Nov 17 '14 at 09:55
  • `SelectParameters` doesn't have a setter, you can't set the list directly. You'd have to iterate `items` and call `ODS.SelectParameters.Add()` for each one. But as the Select Parameters are only consumed in the `Selecting` event, it makes sense to set `SelectParameters` in an event handler. Scott Mitchell has a nice tutorial on this topic here: http://msdn.microsoft.com/en-us/library/aa581787.aspx – Philip Pittle Nov 17 '14 at 10:01
  • Or are you trying to say that you want an instance of a List to be passed as a single Select Parameter? Or you don't want to use an ObjectDataSource at all? You just want to directly bind `items` to a Control (ie GridView?): `myGridViewd.DataSource = items; myGridView.DataBind();` – Philip Pittle Nov 17 '14 at 10:03
  • @Philip Pittle: Exactly , just take I want to put the entire items from the .aspx.cs . – JAN Nov 17 '14 at 10:05

1 Answers1

1

If I understand that you want to call DAL.MyObject.GetPlayersData() and get that data to a data bound control, you don't necessarily need an ObjectDataSource. You can simply wire the data into the DataSource property and then call the DataBind method:

 GridView myGridView;
 //Set Data Source
 myGridView.DataSource = DAL.MyObject.GetPlayersData();
 //Have GridView pull in the data.
 myGridView.DataBind();

Alternatively, if you still want to use some of the features of ObjectDataSource to make two way data binding nicer, you can build up a ObjectDataSource programatically in your code behind. The process is very similiar to the declarative syntax:

  ODS.SelectMethod = "GetPlayersData",
  ODS.DataObjectTypeName = "DAL.MyObject",
  ODS.TypeName = "MyFunctions"

The ObjectDataSource will call GetPlayersData() itself, so you don't need to call it yourself.

But if for some odd reason you need to, you can override the ReturnValue in the Selected event:

 ods.Selected += (sender, args) =>
 {
   //This will cause GetPlayersData() to be called twice
   args.ReturnValue = MyFunctions.GetPlayersData();
 }
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123