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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。