当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net清空控件值的方法(可自定义控件类型)

Asp.net清空控件值的方法(可自定义控件类型)

2017年12月12日  | 移动技术网IT编程  | 我要评论

谢冰冰,街头狩猎巨乳妇女,陆地方舟

由于项目收尾,最近忙着做一些方法的优化,整理了一些分享给大家。

当页面内有许多控件,我们在需要清空其值的时候,一个个清空未免太麻烦。于是写了这么一个方法,可以自定义清空控件的类型,灵活应对业务需求。
复制代码 代码如下:

/// <summary>重置方法控件类型枚举</summary>
/// <remarks>求知域http://www.qqextra.com 2012-12-28</remarks>
public enum resettype
{
/// <summary>
/// textbox
/// </summary>
txt,
/// <summary>
/// dropdownlist
/// </summary>
ddl,
/// <summary>
/// radiobuttonlist
/// </summary>
rbl,
/// <summary>
/// 全部resettype类型
/// </summary>
all
}
/// <summary>重置控件的值</summary>
/// <remarks>求知域http://www.qqextra.com 2012-12-28</remarks>
/// <param name="control">this</param>
/// <param name="rst">resettype.all为清空resettype枚举中包含的所有控件类型</param>
public static void reset(control control, params resettype[] rst)
{
bool bltxt = false;
bool blddl = false;
bool blrbl = false;
foreach (resettype type in rst)
{
if (type == resettype.all)
{
bltxt = true;
blddl = true;
blrbl = true;
break;
}
else
if (type == resettype.txt)
bltxt = true;
else if (type == resettype.ddl)
blddl = true;
else if (type == resettype.rbl)
blrbl = true;
}
foreach (control c in control.controls)
{
//文本框
if (c is textbox && bltxt == true)
{
((textbox)c).text = "";
}
else
//下拉列表
if (c is dropdownlist && blddl == true)
{
dropdownlist ddl = (dropdownlist)c;
if (ddl.items.count > 0)
{
ddl.selectedindex = 0;
}
}
else
//单选按钮列表
if (c is radiobuttonlist && blrbl == true)
{
((radiobuttonlist)c).selectedindex = -1;
}
else
if (c.hascontrols())
{
//递归
reset(c, rst);
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网