Rory Primrose

Learn from my mistakes, you don't have time to make them yourself

View project on GitHub

Binding data to ObjectDataSource parameters

Posted on November 7, 2006

Have you ever needed to have nested databound controls using multiple ObjectDataSources, where the nested ObjectDataSource has a select parameter that requires a databound value itself?

You have no doubt tried the following:

<asp:ObjectDataSource ID="odsCountry" runat="server" SelectMethod="GetCountry" TypeName="CountriesBLL">
    <SelectParameters>
        <asp:Parameter Name="countryCode" DefaultValue='<%# Eval("CountryCode") %>' />
    </SelectParameters>
</asp:ObjectDataSource>

If you try this, you will get an error that says:

Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.Parameter does not have a DataBinding event.

So you can’t bind to your value to the parameter. There is a really simple way around this. Redirect the bound value through a control that does support databinding.

<asp:TextBox runat="server" ID="txtCountryCode" Visible="false" Text='<%# Eval("CountryCode") %>' />
<asp:ObjectDataSource ID="odsCountry" runat="server" SelectMethod="GetCountry" TypeName="CountriesBLL">
    <SelectParameters>
        <asp:ControlParameter Name="countryCode" ControlID="txtCountryCode" PropertyName="Text" />
    </SelectParameters>
</asp:ObjectDataSource>

By doing it this way, the containing ObjectDataSource will provide the required value to the TextBox which is not rendered to the browser. The nested ObjectDataSource that requires the databound value can then obtain the value from the TextBox using a ControlParameter. Easy.