简体   繁体   English

如何从Object的后面的代码而不是aspx的项目列表中插入项目列表?

[英]How to insert into ObjectDataSource a list of items from code behind , and NOT from the aspx?

I have the following declaration in my .ASPX : 我的.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 ? 如何从后面的代码将数据放入ObjectDataSource中?

This : 这个 :

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

Doesn't work. 不起作用

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 . 如果我知道您想调用DAL.MyObject.GetPlayersData()并将该数据获取到数据绑定控件中,则不一定需要ObjectDataSource You can simply wire the data into the DataSource property and then call the DataBind method: 您可以简单地将数据连接到DataSource属性,然后调用DataBind方法:

 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. 另外,如果您仍想使用ObjectDataSource某些功能使双向数据绑定更好,则可以在后面的代码中以编程方式构建ObjectDataSource 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. ObjectDataSource将自己调用GetPlayersData() ,因此您无需自己调用它。

But if for some odd reason you need to, you can override the ReturnValue in the Selected event: 但是,如果出于某些奇怪的原因需要这样做 ,则可以在Selected事件中覆盖ReturnValue

 ods.Selected += (sender, args) =>
 {
   //This will cause GetPlayersData() to be called twice
   args.ReturnValue = MyFunctions.GetPlayersData();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM