CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

ObjectDataSource Programatic Parameters and the DataList

So I've been toying with the new object datasource in our new pet insurance quoting engine we are building and I must say that even though I started with a very wary feeling thanks to Sahil, this control is really growing on me.

The basic scenario in this exercise is that users are filling out a form with pet information that I want to display in a 3 column datalist with a button in the data to remove a pet they have already added.  I wanted to leverage the ObjectDataSource calling a method that provided a generic list of QuotePet objects. 

The datalist itself needed to select the list of added pets by the QuoteId which I could have stored in session etc, but for exploration's sake I decided to figure out how to provide a parameter to the ObjectDataSource that wasn't in the wizard list (ie asking a method for it).

So first we need my datalist and its object datasource which has the select and delete methods defined:

<asp:DataList ID="dlPetlist" runat="server" Width="265px" CellPadding="3" CellSpacing="6" DataSourceID="odsPetList" DataKeyField="Id" RepeatColumns="3" OnDeleteCommand="dlPetlist_DeleteCommand">
            <ItemTemplate>
                <label class="gridLabel">Name: </label>
                <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                <br />
                <label class="gridLabel">Breed: </label>
                <asp:Label ID="BreedLabel" runat="server" Text='<%# Eval("Breed") %>' />
                <br />
                <asp:Label ID="AgeYearsLabel" runat="server" Text='<%# Eval("AgeYears") %>' />
                <label>years</label>
                
                <asp:Label ID="AgeMonthsLabel" runat="server" Text='<%# Eval("AgeMonths") %>' />
                <label>months</label>
                <br />
                <asp:Button ID="Delete" Runat="server" Text="Delete" CommandName="delete" />
                <br />
                <br />
            </ItemTemplate>
        </asp:DataList>

        <asp:ObjectDataSource ID="odsPetList" runat="server" SelectMethod="GetQuotePets"
            DeleteMethod="DeleteQuotePet" TypeName="BasePage" OnSelecting="odsPetList_Selecting">
            <SelectParameters>
                <asp:Parameter Name="quoteId" Type="Object" />
            </SelectParameters>
            <DeleteParameters>
                <asp:Parameter Name="petId" Type="Int32" />
            </DeleteParameters>
        </asp:ObjectDataSource>

So we can see that the datalist highlighted in red the reference to the objectdatasource, and the delete command method in the code-behind.  In the objectdatasource we can see that I've hooked into the OnSelecting event which allows you to make changes to the select parameters, in this case quoteId, before the datasource goes off to do its business.  This is the event to use when you want to set the parameters programatically.  Let's take a look at the odsPetList_Selecting code:

    protected void odsPetList_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)

    {

        e.InputParameters["quoteId"] = this.QuoteId;

    }

Pretty simply really.  The event provides argument e which has a collection of the input parameters that you can set programatically.

The next question is how do we call the delete on the objectdatasource from the datalist?  For this we catch the dlPetList_DeleteCommand event and once again set the parameters for the object datasource and then force it to run its delete method.  For all the description text I just wrote, it's accomplished in all of two lines:

    protected void dlPetlist_DeleteCommand(object source, DataListCommandEventArgs e)

    {

        odsPetList.DeleteParameters["petId"].DefaultValue = Convert.ToString(dlPetlist.DataKeys[e.Item.ItemIndex]);

        odsPetList.Delete();

    }



Comments

Eric Wise said:

I'm multilingual ;).

VB holds a dear place in my heart and remains my language of choice for many situations.  However my current position is in a c# only shop so most of my examples will be in C# in the near future.
# March 23, 2006 10:14 AM

WhoDaPimp said:

<EDITED FOR CONTENT>

Who cares what language its in. If you can't read both by this point you're a second class .NET developer.

# March 27, 2006 11:19 PM

Jeremy said:

I don't understand why two seperate delete methods are defined:

In the datalist you have this:
OnDeleteCommand="dlPetlist_DeleteCommand"

In the objectdatasource you have this:
DeleteMethod="DeleteQuotePet"

Since the datalist is bound to the objectdatasource shouldn't the DeleteQuotePet execute when a control with the command DELETE is clicked?  However this doesn't appear true considering you had to specify OnDeleteCommand="dlPetlist_DeleteCommand" on the DataList which makes the DeleteMethod useless?
# July 6, 2006 4:30 PM

rmk said:

OK, but how do you make this work when you want to use DataTypeObjectName in the objectdatasource, All the events process but when I get to the delete method the object only has initial values, i.e. it's not getting populated from the datalist.

# August 26, 2007 5:31 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications