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