Asp.net清空控件值的方法(可自定义控件类型)
时间:2023-07-22 23:23:16
由于项目收尾,最近忙着做一些方法的优化,整理了一些分享给大家。
当页面内有许多控件,我们在需要清空其值的时候,一个个清空未免太麻烦。于是写了这么一个方法,可以自定义清空控件的类型,灵活应对业务需求。
/// <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);
}
}
}
标签:控件值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
四大因素扼杀了中国人的创造力
2008-09-11 18:05:00
远程登陆SQL Server 2014数据库的方法
2024-01-28 03:39:01
![](https://img.aspxhome.com/file/2023/0/99990_0s.png)
MySQL中ROUND函数进行四舍五入操作陷阱分析
2024-01-15 04:02:04
Mac Homebrew安装的MySQL无法远程登录的解决
2024-01-14 12:12:56
Docker安装MySQL8.0的实现方法
2024-01-23 06:52:06
![](https://img.aspxhome.com/file/2023/1/128001_0s.jpg)
解决python opencv无法显示图片的问题
2021-09-11 13:26:37
![](https://img.aspxhome.com/file/2023/6/131126_0s.jpg)
Vue开发之封装上传文件组件与用法示例
2024-05-10 14:09:44
![](https://img.aspxhome.com/file/2023/4/126574_0s.png)
Python利用PaddleOCR制作个搜题小工具
2022-07-22 16:18:18
![](https://img.aspxhome.com/file/2023/4/68974_0s.jpg)
python 实现查找文件并输出满足某一条件的数据项方法
2021-05-10 11:51:30
PHP常用函数之获取汉字首字母功能示例
2023-06-16 12:24:39
Python爬虫实例爬取网站搞笑段子
2022-11-29 17:14:33
基于XML语言的来实现购物车的详细代码
2008-05-29 13:57:00
Python PyQt5中窗口数据传递的示例详解
2023-12-30 10:14:43
![](https://img.aspxhome.com/file/2023/9/114539_0s.gif)
termux中matplotlib无法显示中文问题的解决方法
2022-12-16 00:07:16
![](https://img.aspxhome.com/file/2023/6/128516_0s.png)
go语言环境搭建简述
2024-05-22 10:12:57
实例学习call、apply、callee用法
2009-03-27 17:56:00
python获取http请求响应头headers中的数据的示例
2023-06-30 14:49:02
![](https://img.aspxhome.com/file/2023/1/133911_0s.png)
详解Mysql多表联合查询效率分析及优化
2024-01-23 00:59:29
Oracle 数据库操作技巧集
2010-07-26 12:49:00
Python如何优雅删除字符列表空字符及None元素
2023-10-26 19:17:00