locked
Binding an ObjectDataSource to a property in aspx code, not code-behind RRS feed

  • Question

  • User-1202708140 posted

    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;
       odsBooks.DataBind();
    }

    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"
         DataObjectTypeName="BookCatalogue.InteractionDocuments.Book" 
         DataSource="SelectedBook"/>

    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?

    Thursday, December 2, 2010 8:20 PM

Answers