关于form表单中禁用所有Asp.net 服务器控件TextBox的方法

来源:互联网 发布:史上最好玩的游戏不需要网络 编辑:程序博客网 时间:2024/05/21 06:01

调用方法:CancelFormControlEnterKey(this.Page.Form.Controls);


/// <summary>

        /// 
        /// </summary>
        /// <param name="controls"></param>
        public static void CancelFormControlEnterKey(ControlCollection controls)
        {
            foreach (Control item in controls)
            {
                //服务器TextBox
                if (item.GetType() == typeof (TextBox))
                {
                    WebControl webControl = item as WebControl; webControl.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} "); 
                }
                //html控件   
                else if (item.GetType() == typeof(HtmlInputText))
                {
                    HtmlInputControl htmlControl = item as HtmlInputControl;
                    htmlControl.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} ");
                }
                //用户控件   
                else if
                (item is System.Web.UI.UserControl)
                {
                    CancelFormControlEnterKey(item.Controls);
                    //递归调用   
                }
            }
        }
阅读全文
0 0