views:

172

answers:

2

Hi,

I have a control, and I want to interate through its datasource, is that possible?

The control is bound to a repeater, but I also want to loop through the items using inline code on the top of the .ascx control page.

Is this possible?

Update

I am binding to a repeater, and the thing bound to the repeater is a List collection.

I tried:

myRepeater.DataSource

And I don't get anything via intellisense, casting to List<UserPRofile> doesn't work either.

A: 

Sure, just refer to the control's DataSource property and it will return whatever you originally bound to the control. You'll want to cast the return value to the appropriate type.

var dataSource = (List<MyClass>)myRepeater.DataSource; // your control

Bear in mind that if you plan to make changes to it and want it to be reflected in the control then you should use an appropriate datasource that supports 2-way binding, such as a BindList.

Ahmad Mageed
Not working, not even after casting it...
mrblah
Can you update your question with some more code? Show us the Repeater markup and the databinding. It also sounds like you're using a user control. Are you binding the list to the user controls and attempting to access their datasources? That would be separate from the repeater's datasource.
Ahmad Mageed
A: 

As I understand you want to access a repeater's datasource that located inside an user control. I recommend you to expose the datasource as a property and use it to loop outside the user control as well as to bind your repeater.

Also you can use Repeater.Items collection and get each binded row as RepeaterItem instance.

foreach (RepeaterItem repeaterItem in myRepeater.Items)
{
    lblResult.Text += " " + repeaterItem.Text;
}
Canavar