Nov 7 2006

Binding data to ObjectDataSource parameters

Category: IT Related | .NetRory Primrose @ 12:51

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 [:)].

Tags:

Comments (8) -

1.
am am says:

I so grateful for this hint (very clean, pure declarative syntax) that I'll enrich it with additional tip: remember to put the textbox before the gridview and its datasource in DOM hierarchy, because asp.net binds controls in order they are laid in HTML and if it tries to bind GV before the textbox it will be empty (until refresh, maybe, provided the textbox has the ViewState enabled, in such case it will already have its value set in next databinding cycle).

thanks again
greets

2.
Rory Primrose Rory Primrose says:

Thanks am, that's a great tip.

3.
Prince Ashif Raich Prince Ashif Raich says:

helpful.

4.
Juan Pablo Juan Pablo says:

Hey
Great. This work perfect!!!!

5.
goodcat goodcat says:

great stuff

6.
Ferdinand Prantl Ferdinand Prantl says:

Thanks for that, hels even today Smile
I would just use a special control made for this purpose:

8.
Effetto Effetto Russia says:

Thanks for the material, very helpful.
But i used HiddenField for this method.

9.
Rory Primrose Rory Primrose Australia says:

Hi Effetto,

It looks like HiddenField is a better solution that the one proposed here (keeping in mind that this was posted back in 2006). The MSDN documentation says that this is new in the 3.0 framework. It's good to see that there is now support for this natively in the framework.

Pingbacks and trackbacks (3)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading