开发者

How can I access the objects I've assigned to Repeater.DataSource?

开发者 https://www.devze.com 2023-03-21 01:01 出处:网络
I have a Repeater Control which I bind to a list of Products Repeater.DataSource = ProductRepository.GetProducts();

I have a Repeater Control which I bind to a list of Products

Repeater.DataSource = ProductRepository.GetProducts();

Later (in another module), I need to retrieve the products from the Repeater, but it seems not to work with Repeater.Item.DataItem (which is then null). My question is, is there any reference to the products in the开发者_JS百科 Repeater-Items? Or do I have to set it myself via id field etc. What would be the easiest way to get the "underlying" product objects?


No there aren't references to the products in the repeater items. You have to iterate trough the repeater items and create the objects yourself.


When do you need the product item? If you have event handlers attached for repeater item databound, created etc events then you will get the product item in each of these events.


The databinding is used to populate the control. Once the control has been populated with data, the underlying data is disposed. If you want to access data entered by the user, you reference via the rows in the repeater and the control id's containing values. If you need access to data that you don't want to display to the user, place in a hidden Label.

foreach (RepeaterItem item in Repeater.Items)
{
    string value1 = (item.FindControl("MyTextBox1") as TextBox).Text;
    string value2 = (item.FindControl("MyTextBox2") as TextBox).Text;
    string value3 = (item.FindControl("MyLabel1") as Label).Text;

    // do something with retrieved values.
}

Hope this helps!


You can access Repeater.Item.DataItem in ItemDataBound event of Repeater. Otherwise it is null. Your data is not supposed to be preserved with post-backs, but your controls state. So your best shot is to keep your data in session or viewstate.

0

精彩评论

暂无评论...
验证码 换一张
取 消