Is there any listbox control on the page?

2019-08-10 11:59发布

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

2条回答
三岁会撩人
2楼-- · 2019-08-10 12:32

You can try like...

if (Page.Controls.OfType<ListBox>().Count() > 0)
   {
       Response.Write("Listbox control exist");
   }
查看更多
你好瞎i
3楼-- · 2019-08-10 12:53

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);

          }

      }

 }

}
查看更多
登录 后发表回答