Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid cast exception C# ASP.Net

foreach(Label l in Controls)    // setting all labels' s visbility  on page to true
     l.Visible =true;

But on running ,i am getting following error

Unable to cast object of type 'ASP.admin_master' to type 'System.Web.UI.WebControls.Label'.

like image 865
HalfWebDev Avatar asked Jan 25 '26 12:01

HalfWebDev


1 Answers

If one of the controls is not of type label, you'll get that error.

You could try:

foreach(Label l in Controls.OfType<Label>())
{
    l.Visible = true;
}
like image 180
Philippe Leybaert Avatar answered Jan 28 '26 01:01

Philippe Leybaert