Search code examples
asp.netobjectdatasource

Pass value from ObjectDataSource object level to ASP .NET Control


I'm working with ObjectDataSource and FormView control.

In ObjectDataSource I have a type specified, and a method that performs Insert operation. However, once Insert is done, I would like to redirect the user to other page, and I need ID of newly created/persisted object.

So in FormView:

OnItemInserted="OnFormItemInserted"

handler I need to access that ID. But how to pass that id gracefully from ObjectDataSource level? I could use

HttpContext.Current.Items

inside ObjectDataSource handling object, but I don't like it, as my handling type do not know anything about ASP .NET (separation of concerns?).

Thanks


Solution

  • Have you tried something simple like this:

    In your Data object

    Update your insert method configured in the ObjectDataSource control to return the generated ID:

    public int Insert(string firstName, int lastname)
    {
        // return the ID generated by the insert command
        return 4;
    }
    

    ASPX code behind

    And that's it, in your page where you are handling the Inserted event:

    protected void ods_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {
        var res = e.ReturnValue.ToString();
        this.lblMessage.Text = res;
        // add your cool additional logic and redirect stuff
    }