黑马程序员技术交流社区
标题:
asp.net中遍历界面上所有控件进行属性设置
[打印本页]
作者:
sunrise2
时间:
2014-8-11 09:36
标题:
asp.net中遍历界面上所有控件进行属性设置
#region 遍历界面上所有控件进行属性设置
/// <summary>
/// 遍历界面上所有控件进行属性设置
/// </summary>
/// <param name="page"></param>
/// <param name="type">
///isClear是添加时候,清空数据信息,如果该控件为只读属性则不需要清除文本数据信息,
///如果type参数为空数值则默认为查看状态,控件都全部禁用掉
/// </param>
public static void initControl(Control page, string type)
{
int nPageControls = page.Controls.Count; //获取页面的控件
for (int i = 0; i < nPageControls; i++)
{
foreach (Control control in page.Controls[i].Controls)
{
{
//文本框控件
if (control is TextBox)
{
TextBox txtBox = (TextBox)control;
//如果是点击重置,需要判断是否为只读属性,如果是则不进行清除数据
if (type == "isClear" && txtBox.Enabled != false)
txtBox.Text = "";
else
txtBox.Enabled = false;
}
//下拉框控件
if (control is DropDownList)
{
DropDownList ddlList = (DropDownList)control;
if (type == "isClear" && ddlList.Enabled != false)
ddlList.SelectedIndex = -1;
else
ddlList.Enabled = false;
}
//复选框控件
if (control is CheckBox)
{
CheckBox chkBox = (CheckBox)control;
if (type == "isClear" && chkBox.Enabled != false)
chkBox.Checked = false;
else
chkBox.Enabled = false;
}
//点击按钮
if (control is Button)
{
Button btn = (Button)control;
if (type == "isClear" && btn.Enabled != false)
btn.Enabled = true;
else
btn.Enabled = false;
}
if (control is RadioButtonList)
{
RadioButtonList radioList = (RadioButtonList)control;
if (type == "isClear" && radioList.Enabled != false)
radioList.SelectedIndex = -1;
else
radioList.Enabled = false;
}
}
}
}
}
#endregion
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2