How to set ObjectDataSource Parameters programmatically?

 Copyright © 2007-2012 www.AspDotNetFaq.com

Let us first explain a little how ObjectDataSource component works:

when ObjectDataSource needs to retrieve data (from Database or any other source) it first raises Selecting event.
After this event fires, the data is retrieved using the command declared in SelectMethod property.
After this data is retrieved, Selected event is raised.

Logically, right place to programmatically set, add or change parameters that will be used by SelectMethod is Selecting event.

here is a code snippet that does this:

    protected void faqsDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["categoryId"] = CategoryId;
        e.InputParameters["startRowIndex"] = StartRowIndex;
        e.InputParameters["maximumRows"] = ItemsPerPage;
    }

The same applies for Inserting, Updating and Deleting data.

ObjectDataSource has similar events fired for these operations, and they are called:

Updating and Updated
Deleting and Deleted
Inserting and Inserted.

These events can also be used to change parameters that will ObjectDataSource use for its operation.


 Copyright © 2007-2012 www.AspDotNetFaq.com