asp.net中遍历界面上所有控件进行属性设置

 * 使用方法:
 *  前台页面调用方法,重置:
    protected void Reset_Click(object sender, EventArgs e)
        {
            initControl(Page, "isClear");
        }

 * 备注信息: 上传部分自己总结的常用方法的封装,有不足和不完美之处,希望大家指出来,愿意一起
 * 主要研究erp,cms,crm,b2b,oa等系统和网站的开发,欢迎有共同追求和学的IT人员一起学习和交流。
 * 学习和讨论有关asp.net  mvc ,Ajax ,jquery ,html/css, xml ,sqlserver ,wpf,IIS以及服务器的搭建和安全性相关技术的交流和学习。

1. [代码][C#]代码 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

佛为心,道为骨,儒为表,大度看世界; 技在手,能在身,思在脑,从容过生活; 三千年读史,不外功名利禄; 九万里悟道,终归诗酒田园;
原文地址:https://www.cnblogs.com/taofx/p/4137267.html