在Button_OnClick事件中访问转发器数据项

问题描述 投票:2回答:1

我有一个绑定到对象集合的转发器控件。当我触发button_onclick事件时,我需要访问数据项以获取对象属性。这是我所拥有的,我的问题是如何在button_onclick事件中访问转发器中的基础对象

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
asp.net repeater buttonclick
1个回答
4
投票

未保留数据项;它仅用于绑定初始接口,除非您在每次页面加载时都重新绑定转发器。然后,需要为按钮提供一个命令名值,然后点击repeater.itemCommand,这将使您能够访问存在dataitem属性的转发器项。

EDIT:如果需要访问转发器中的项目,则可以执行以下操作:

foreach (var item in this.rpt.Items)
{
   if (item.DataItem != null) {
      //Do something
   }
}

您是否要访问一行或行的集合?

HTH。

© www.soinside.com 2019 - 2024. All rights reserved.