views:

34

answers:

3

I know you can bind in the code-behind, but I don't quite understand how it works if you want to declare it within the controls tag. This is for a custom collection.

I thought it was just

Datasource="<%# MyCollection %>"

Where MyCollection is an exposed property of the class in the code behind?

MyCollection{ get{ return _MyCollection;}}

Something like that. But it doesn't seem to work that way, so can I not bind this way, or am I missing something?

A: 

Clarify what control do you use. But anyway - <%# %> - is just way to say you want extract data from data bound container. But in your case it not so (you haven't outer container).

To fix your problem you need at page load just assign

MyControl.Datasource = MyCollection
Dewfy
It was just generic controls which have the datasource property. Repeater, DropDownList etc
Psytronic
A: 

The 'DataSource' property cannot be set declaratively. Is there a really reason to do this instead of binding in the code behind?

The alternate way to do this is by using the DataSourceID instead of DataSource to set it declaratively, The DataSourceID must be the ID of a control of type IDataSource like SqlDataSource. You need to make a custom control that implements this interface and provide it as datasource, but again I don't see a real good reason to do this

Spyros
No, no real reason, just so it wasn't filling out the code behind. And curiosity.
Psytronic
A: 

You should be able to do what you have said, yes.

http://support.microsoft.com/kb/307860

I think the bit you've missed is this (quoted from the above link):

After the particular data sources have been determined and set for the objects on the .aspx page, you must bind the data to these data sources. You can use the Page.DataBind or the Control.DataBind method to bind the data to the data sources.

IIRC I think you don't have to do that, if you want to have a databound control within a databound control - e.g. nested repeaters. In that situation I think setting the DataSource is enough but I may be corrected?

Have a look, at the section entitled "Page.DataBind() versus Control.DataBind()"

Another page with some examples is here:
http://odetocode.com/articles/278.aspx

bgs264