I have a repeater control bound to a collection of objects. When I fire off the button_onclick event, I need to have access to the dataitem to get the object properties. Here is what I have and my question is how do I access the underlying objects in a repeater in a button_o开发者_Go百科nclick event
protected void OKButton_Click(object sender, EventArgs e)
{
try
{
string selectedValue = Request.Form["repeaterRadioButton"];
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
MyObject myObject = (MyObject)item.DataItem;
if (!string.IsNullOrEmpty(selectedValue) && selectedValue == myObject.MyProperty)
{
//stuff in here
} ... rest of code
The dataitem is not retained; it's only used to bind the initial interface, unless you rebind the repeater on every page load. Then you need to give the button a commandname value, and tap into the repeater.itemCommand, which will give you access to the repeater item, where the dataitem property lives.
EDIT: If you need to access items within the repeater, you can do:
foreach (var item in this.rpt.Items)
{
if (item.DataItem != null) {
//Do something
}
}
Are you trying to access one row or the collection of rows?
HTH.
精彩评论