由于项目收尾,最近忙着做一些方法的优化,整理了一些分享给大家。
当页面内有许多控件,我们在需要清空其值的时候,一个个清空未免太麻烦。于是写了这么一个方法,可以自定义清空控件的类型,灵活应对业务需求。
复制代码 代码如下:
/// <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);
}
}
}
当页面内有许多控件,我们在需要清空其值的时候,一个个清空未免太麻烦。于是写了这么一个方法,可以自定义清空控件的类型,灵活应对业务需求。
复制代码 代码如下:
/// <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);
}
}
}
标签:
控件值
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“Asp.net清空控件值的方法(可自定义控件类型)”评论...