csharper
csharper

Reputation: 13

Is there any listbox control on the page?

I want to learn that in asp.net, how can we understand is there any listbox control on the page programmatically?

Upvotes: 1

Views: 146

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

You can try like...

if (Page.Controls.OfType<ListBox>().Count() > 0)
   {
       Response.Write("Listbox control exist");
   }

Upvotes: 1

Ashley John
Ashley John

Reputation: 2453

You need to check in a Page's control collection recursively

int count =0;
private void FindControl(Control Page)

 {


 foreach (Control ctrl in Page.Controls)

 {

      if (ctrl is ListBox)

      {

         count++;
      }

      else 

      {

          if (ctrl.Controls.Count > 0)

          {

               FindControl(ctrl);

          }

      }

 }

}

Upvotes: 0

Related Questions