I have a page with some datasources in it. I am doing reenginering of the page itself and would like to split some parts of the page in several user controls.
In my code I have an ObjectDataSource object with the following params
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="DataSetTableAdapters.PhotosTableAdapter"
SelectMethod="GetPhotosForAlbum">
<SelectParameters>
<asp:ControlParameter Name="albumID" ControlID="txtAlbumID" Type="Int32"/>
</SelectParameters>
</asp:ObjectDataSource>
This piece of code automatically bind the hidden field “txtAlbumId” to the datasource. In my revision this datasource should be moved to a user control.
What is the best way to handle a scenario like this?
Regards,
Lorenzo
You can create a property on the user control to expose the property on the hidden field like so:
You should then be able to wire the object data source to the user control:
Edit:
There are a few ways to go the other way around (i.e., data source in the user control). One approach is viable, but I find makes for some designer confusion, is to expose the parameter collection from the user control as a public property:
Doing so allows you to fill the parameter collection from the page like so:
If you use a designer, your control will always display an error, but it works. (Note, that I’ve used this approach with query string and session parameters. It would be interesting to see if it will survive control parameters.)
I think, though, that the easiest method is to swap out your
ControlParameterfor a plain oldParameter, and expose the default value through a property.You can then data bind or assign directly to the AlbumID property of the control, and expect the value to be forwarded to the parameter.