views:

10

answers:

2

Hello, 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

+1  A: 

You can create a property on the user control to expose the property on the hidden field like so:

public int AlbumID
{
    get { return Convert.ToInt32(txtAlbumID.Value); }
    set { txtAlbumID.Value = value.ToString(); }
}

You should then be able to wire the object data source to the user control:

<asp:ControlParameter Name="albumID" ControlID="myUserControl" PropertyName="AlbumID" Type="Int32"/>

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:

public ParameterCollection SelectParameters
{
    get { return ObjectDataSource1.SelectParameters; }
}

Doing so allows you to fill the parameter collection from the page like so:

<my:UserControl runat="server" ID="myUserControl">
    <SelectParameters>
        <%-- ... --%>
    </SelectParameters>
</my:UserControl>

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 ControlParameter for a plain old Parameter, and expose the default value through a property.

public int AlbumID
{
    get { return Convert.ToInt32(ObjectDataSource1.Parameters[0].DefaultValue); }
    set { ObjectDataSource1.Parameters[0].DefaultValue = value.ToString(); }
}

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.

kbrimington
I was not perfectly clear on my question sorry. The datasource should be moved inside the user control but the hidden field will continue to stay on the aspx page. Reading your solution seems the opposite.
Lorenzo
@Lorenzo - Sorry for the misunderstanding. I added a few strategies for the opposite approach.
kbrimington
Great! Thanks a lot!!
Lorenzo
A: 

Another option would be to just do it programatically.

var cp = ObjectDataSource1.SelectParameters["albumID"] as ControlParameter;

cp.ControlID = //Any one of a dozen ways to get this value into the control
Josh
Yes. This is sure a solutions. But before use code to programmatically set that value on the user control I would like to explore if there are solutions that with declarative code solve the problem. Thanks anyway!
Lorenzo