I'm trying to use an ObjectBindingSource to bind data to a gridview. The data is present in the view as a subclass as the controller, in the form of a model.
var _controller = new DataController(param1, param2);
you would access it in this manner
foreach(var Variable in _controller.DataModel.Cars)
<%: Car.Name %> ...
How can I use this DataModel.Cars as the source for the Object binding source?
I currently have
<asp:开发者_开发问答ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="Choices.ChoiceService.ChoiceCollection"
DataObjectTypeName="Choices.ChoiceService.ChoiceObject"
InsertMethod="Add" SelectMethod="AsReadOnly"/>
Insert a Select Method on DataController say GetCars this way you would do:
public class DataController
{
public IEnumerable<Car> GetCars(int param1, int param2)
{
return this.DataModel.Cars;
}
}
and then you would use:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="Choices.ChoiceService.DataController"
DataObjectTypeName="Choices.ChoiceService.Car"
SelectMethod="GetCars">
<SelectParameters>
<asp:SessionParameter Name="param1" SessionField="param1" />
<asp:SessionParameter Name="param2" SessionField="param2" />
</SelectParameters>
</asp:ObjectDataSource>
You then set the session on the codebehind witht:
Session["param1"] = 12;
Session["param2"] = 13;
Note:There are much better ways to do this, using the session like this is really ugly, this is only an example. You probably can pick a better one from the following list:
http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx
Alternatively you can use the OnDataBinding event of the ObjectDataSource to programatically set the parameters directly on the ObjectDataSource instance.
精彩评论