---
title: "遍历页面上的下拉框元素，设置初始值的4种写法"
author: "starnc"
date: "2009-04-30T14:51:13Z"
tags: []
slug: "4"
draft: false
lastmod: "2009-05-05T17:29:56Z"
id: 377378
url: "https://starnc.iteye.com/blog/377378"
---

```
            foreach (Control controls in Page.Form.Controls)
            {
               
                if (controls is System.Web.UI.WebControls.DropDownList  )
                {
                    System.Web.UI.WebControls.DropDownList c = (DropDownList)controls;
                    c.Items.Add(new ListItem("所有","所有"));
                }
            }
```

 

```
 foreach (Control controls in Page.Form.Controls)
            {
               
                if (controls.GetType()=="System.Web.UI.WebControls.DropDownList"  )
                {
                    System.Web.UI.WebControls.DropDownList c = (DropDownList)controls;
                    c.Items.Add(new ListItem("所有","所有"));
                }
            }
```

 

```
 foreach (Control controls in Page.Form.Controls)
            {
               
                if (controls.GetType()== typeof(System.Web.UI.WebControls.DropDownList))
                {
                    System.Web.UI.WebControls.DropDownList c = (DropDownList)controls;
                    c.Items.Add(new ListItem("所有","所有"));
                }
            }
```

 

```
foreach (Control controls in Page.Form.Controls)
            {
               
                if (controls.GetType().Equals(typeof(System.Web.UI.WebControls.DropDownList)))
                {
                    System.Web.UI.WebControls.DropDownList c = (DropDownList)controls;
                    c.Items.Add(new ListItem("所有","所有"));
                }
            }
```

 以上4种写法都可以，我也是主要学习一下typeof和gettype（）的用法