As System.Web.UI.AttributeCollection does not have a public definition for GetEnumerator so the only way that you have is just using Keys property of this class in order to get all attributes' keys
public void CheckControls(ControlCollection controls)
{
foreach (Control control in controls)
{
WebControl ctr = control as WebControl;
if (ctr != null)
{
foreach (string key in ctr.Attributes.Keys)
{
if (key == "sth")
{
string dataAction = ctr.Attributes[key].Trim().ToLower();
control.Visible = false;
}
}
if (control.HasControls() == true)
CheckControls(control.Controls);
}
}
}