I am trying to pass a collection of Dropdown controls as a parameter to a method which takes a collection of type Control as input. While executing I get the following error:
“Unable to cast object of type ‘d__a31[System.Web.UI.WebControls.DropDownList]' to type 'System.Collections.Generic.IEnumerable1[System.Web.UI.Control]’.”
Any idea why am I getting this?
My code:
private void Caller()
{
IEnumerable<DropDownList> dropDownControlsInCurrentRow = currentRow.Controls.OfType<DropDownList>();
SetControlsVisibility(dropDownControlsInCurrentRow, false);
}
private void SetControlsVisibility(IEnumerable<Control> controlCollection, bool visibilityFlag)
{
foreach (ctrl in controlCollection) {
ctrl.Visible = visibilityFlag;
}
}
use
instead of