ASP.NET控件遍历详解

最近在学习开发的时候,总是遇到判定一个有关用户输入页面中输入是否合法的情况,开始时也没有好好地想一下,就不管三七二十一地用最原始方法来一个个地查找判断,写了半天才定写完,而且效率也不是很高。在网上看了别人的方法后,才知道我原来的方法是多么的的幼稚,说了这么多,我还是把我原来的方法和改进的方法做一个比较吧,各位也可以看一下,觉得自己是不是以前也有类似的经历。

其验证的窗体如下所示:

 

       

        private bool ValidateInput()
        {
            if (this.txtUserName.Text == "")
            {
                MessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtUserName.Focus();
                return false;
            }
            if (this.txtPwd.Text == "")
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtPwd.Focus();
                return false;
            }
            if (this.txtConfirmInfo.Text == "")
            {
                MessageBox.Show("请输入确认密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtConfirmInfo.Focus();
                return false;
            }
            if (!(this.txtPwd.Text.Trim() == this.txtConfirmInfo.Text.Trim()))
            {
                MessageBox.Show("两次输入的密码不一致", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtConfirmInfo.Focus();
                return false;
            }
            if (this.combClass.Text == "")
            {
                MessageBox.Show("请选择用户班级", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.combClass.Focus();
                return false;
            }
            if (this.txtTrueName.Text == "")
            {
                MessageBox.Show("请输入学员姓名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtTrueName.Focus();
                return false;
            }
            if (this.txtUserID.Text == "")
            {
                MessageBox.Show("请输入学号", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtTrueName.Focus();
                return false;
            }
            if (!this.ratBoy.Checked && !this.ratGirl.Checked)
            {
                MessageBox.Show("请选择学员性别", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ratBoy.Focus();
                return false;
            }
            if (!this.rbtActive.Checked && !this.rbtDynamic.Checked)
            {
                MessageBox.Show("请设置用户的状态", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.rbtActive.Focus();
                return false;
            }
            return true;
        }

这个函数采用最普通的方法来验证,方法很简单,用着也比较顺手。

第二各方法如下:

 private bool CheckInputMethod()
        {
            foreach (TabPage tp in this.tabCreateStudent.TabPages)
            {
                foreach (Control ctr in tp.Controls)
                {
                    if (ctr is CheckBox)
                    {
                        if (!(((CheckBox)ctr).Checked))
                        {
                            MessageBox.Show("请选择状态", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            ((CheckBox)ctr).Focus();
                            return false;
                        }
                    }
                    if (ctr is TextBox)
                    {
                        if (((TextBox)ctr).Text == "")
                        {
                            MessageBox.Show("文本框不能为空,请核查!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            ((TextBox)ctr).Focus();
                            return false;
                        }
                    }
                    if (ctr is ComboBox)
                    {
                        if (((ComboBox)ctr).Text == "")
                        {
                            MessageBox.Show("列表框不能为空,请核查!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            ((ComboBox)ctr).Focus();
                            return false;
                        }
                    }
                }
            }
            return true;
        }

这种方法较上面的一种可以说是改进了不少,在验证有很多控件有效性的窗体中可以说也是效率较高的,但要考虑的一点问题是要考虑遍历的控件所在的当前容器

第三和方法如下:

        private bool InputChecked(Control ctr,int notValidator)
        {
            for (int i = 0; i < ctr.Controls.Count; i++)
            {
                if (ctr.Controls[i].GetType() == typeof(TextBox))
                {
                    if (((TextBox)ctr.Controls[i]).Text == "")
                    {
                        MessageBox.Show("输入不能为空,请核查!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ((TextBox)ctr.Controls[i]).Focus();
                        notValidator++;
                    }
                }
                if (ctr.Controls[i].GetType() == typeof(CheckBox))
                {
                    if (!((CheckBox)ctr.Controls[i]).Checked)
                    {
                        MessageBox.Show("请设置用户的状态", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ((CheckBox)ctr.Controls[i]).Focus();;
                        notValidator++;
                    }
                }
                if (ctr.Controls[i].GetType() == typeof(ComboBox))
                {
                    if (((ComboBox)ctr.Controls[i]).Text=="")
                    {
                        MessageBox.Show("列表框内容不能为空!", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ((ComboBox)ctr.Controls[i]).Focus();
                        notValidator++;
                    }
                }
                if (ctr.Controls[i].Controls.Count > 0)
                {
                     InputChecked(ctr.Controls[i]);
                }
            }
            return notValidator>0 ? false:true;
        }

这种方法用递归的方法循环调用本身,用一个整形变量来记录不合法的控件,如果有不合法的控件就让这个整形变量自加,到最后如果这个变量大于0,就说明有不合法的控件存在,就返回FALSE,角绍到这里大家都应该清楚了吧,对于以后工作中遇到类似的情况我想大家都能够灵活处理了。

http://www.cnblogs.com/DotNetNuke/archive/2008/05/29/1206520.html

posted @ 2009-10-22 14:41  window5549-accp  阅读(604)  评论(1编辑  收藏  举报