C# 使用 foreach 获取中继器数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8915758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:09:30  来源:igfitidea点击:

Get Repeater data with foreach

c#asp.netrepeater

提问by mcamara

I have a Repeater in my page and after databinding, I have to click on a button to postback in page, and I need to do a foreachin all data from my Repeater. In true I have to get each item inside foreachstatment as example.

我的页面中有一个中继器,在数据绑定后,我必须单击一个按钮在页面中回发,并且我需要foreach从我的中继器中执行所有数据。实际上,我必须将每个项目放入foreachstatment 作为示例。

foreach (RepeaterItem itemEquipment in rptSpecialEquipments.Items)
{
   // Get Data From My Repeater
}

Best Regards,

此致,

Milton Camara Gomes

米尔顿·卡马拉·戈麦斯

采纳答案by gabsferreira

Is this what you want?

这是你想要的吗?

    foreach (RepeaterItem itemEquipment in rptSpecialEquipments.Items)
    {
        //to get the dropdown of each line
        DropDownList yourDropDown = (DropDownList)item.FindControl("the name of your dropdown control here");

        //to get the selected value of your dropdownlist
        string value = yourDropDown.SelectedValue;
    }

回答by Ashish Yadav

when you are declaring RepeaterItem as itemEquipment then (dropDownList) should be be found in itemEquipment not item

当您将 RepeaterItem 声明为 itemEquipment 时,应该在 itemEquipment 而不是 item 中找到 (dropDownList)

so correct code would be as below. I tried to edit the answer above but the person who reviewed it rejected by edition.

所以正确的代码如下。我试图编辑上面的答案,但它的人被版本拒绝了。

foreach (RepeaterItem itemEquipment in rptSpecialEquipments.Items)
    {
        //to get the dropdown of each line
        DropDownList yourDropDown = (DropDownList)itemEquipment.FindControl("the name of your dropdown control here");

        //to get the selected value of your dropdownlist
        string value = yourDropDown.SelectedValue;
    }