Get Repeater data with foreach

2019-07-09 09:05发布

问题:

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 foreach in all data from my Repeater. In true I have to get each item inside foreach statment as example.

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

Best Regards,

Milton Câmara Gomes

回答1:

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;
    }


回答2:

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

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;
    }