Last post Dec 03, 2010 10:24 AM by PeteNet
Dec 02, 2010 08:20 PM|MightyC|LINK
This should be simple to do, but I haven't been able to find a solution.
I want to bind an ObjectDataSource to a property in my asp page and I want to do it in the page's aspx code, not in code-behind. I've already done it in code-behind, as follows:
protected void Page_InitComplete(object sender, EventArgs e)
odsBooks.DataSource = SelectedBook;
I'd prefer to get rid of the ugly code-behind and just do this in mark-up. Something like
<asp:ObjectDataSource ID="odsBooks" runat="server"
This gives me a run-time error: Cannot create an object of type 'System.Object' from its string representation 'SelectedBook' for the 'DataSource' property.
Is there a way of doing this declaratively and not in code-behind?
Further, what if what I want is to actually use a child property of the property in question? For example, if my Book object has a ReaderComments collection, can I databind to it decaratively in the aspx mark-up?
Dec 03, 2010 10:24 AM|PeteNet|LINK
The ObjectDataSource doesn't have a DataSource property, (reference:
ObjectDataSource Properties) ....so are you into a custom ODS (inheriting from)? also, as such, you'd need to use binding expressions like <%# ... %> for server control properties.
It would depend on what the real objective is, but if you're trying to set an instance of your data object to override the ODS constructing by itself, you could look into the ObjectCreating event: