温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Asp.net中怎么清空控件值

发布时间:2021-07-15 15:54:54 来源:亿速云 阅读:236 作者:Leah 栏目:开发技术

本篇文章为大家展示了Asp.net中怎么清空控件值,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。


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

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

上述内容就是Asp.net中怎么清空控件值,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI