遍历页面上的下拉框元素,设置初始值的4种写法
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()的用法
已有 2 人发表留言,猛击->>这里<<-参与讨论
JavaEye推荐
- 高薪招聘架构师和资深软件设计师职位(上海或北京)
- 【杭州】高薪招聘java高级工程师,项目经理,架构师
- 参与阿里旺旺创意大赛,奖品总额高达40万,你还在等什么?
- 【杭州】高薪招聘控件高级开发工程师(9w-18w年薪)
浙公网安备 33010602011771号