Question

I want to know if it's possible to find a control in a repeater but by approximation. I mean, I have some controls with end "....EditMode" and I want to catch them all and modify some attribute

Something like this

foreach(RepeaterItem item in repeater1.Items)
{
     HtmlGenericControl divEditMode = item.FindControl("....IndexOf ("EditMode")");
     if(divEditMode != null)
     {
          divEditMode.Visible = false;
     }
}
Était-ce utile?

La solution

foreach(RepeaterItem item in repeater1.Items)
{
  foreach (var control in item.Controls)
  {
     if(control.ID.EndsWith("EditMode"))
     {
          control.Visible = false;
     }
  }
}

If I understand your wishes.

Autres conseils

The way to accomplish this is to loop through the controls "by hand" instead of using FindControl. You can use the Controls collection of the RepeaterItem to list all controls and analyze their ids.
As controls are organized as a tree, you should recursively inspect also the Controls collections of the controls on the top level.

private IEnumerable<Control> GetEditControls(ControlCollection controls)
{
    var lst = new List<Control>();
    if (controls == null)
        return lst;
    foreach(var ctrl in controls)
    {
        if (ctrl.Id.EndsWith("EditMode"))
           lst.Add(ctrl);
        lst.AddRange(GetControls(ctrl.Controls);
    }
    return lst;
}
// ...
foreach(RepeaterItem item in repeater1.Items)
{
     var divsEditMode = GetEditControls(item.Controls);
     foreach(var divEditMode in divsEditMode)
     {
          divEditMode.Visible = false;
     }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top