更好的方法?查找ASP.NET控件,找到其ID [英] A Better way? Finding ASP.NET controls, finding their id

查看:59
本文介绍了更好的方法?查找ASP.NET控件,找到其ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法可以找到所有控件,对其进行迭代,确定它们是否为文本框,下拉列表等.检索其ID名称,并根据ID名称设置一个布尔语句(因此我想知道表单的这一部分是否完整,并且会通过电子邮件发送给特定人群.不幸的是,这是用太多的if语句完成的,并且想知道我是否可以得到一些帮助,以使其更易于管理

I have a method that finds all the controls, iterates through them, determines if they are a textbox,drop down list, etc.. retrieves their ID name, and depending on the ID name it will set a boolean statement (thus I would know if that section of the form is complete, and will email to a certain group of people) unfortunetly this is done with too many if statements and was wondering if I could get some help making this more manageable

protected void getEmailGroup()
{
    Control[] allControls = FlattenHierachy(Page);
    foreach (Control control in allControls)
    {
        if (control.ID != null)
        {
            if (control is TextBox)
            {
                TextBox txt = control as TextBox;
                if (txt.Text != "")
                {
                    if (control.ID.StartsWith("GenInfo_"))
                    {
                        GenInfo = true;
                    }
                    if (control.ID.StartsWith("EmpInfo_"))
                    {
                        EmpInfo = true;
                    }
                }
            }
            if (control is DropDownList)
            {
                DropDownList lb = control as DropDownList;
                if (lb.SelectedIndex != -1)
                {
                    if (control.ID.StartsWith("GenInfo_"))
                    {
                        GenInfo = true;
                    }
                    if (control.ID.StartsWith("EmpInfo_"))
                    {
                        EmpInfo = true;
                    }
                }
            }
        }
    }
}      

推荐答案

我没有使用Lambda表达式,而是创建了一个为我处理该控件的方法,并根据控件的名称将其设置为真实

Instead of using the Lambda expression I have created a method that handles the control for me, and depending on the name of the control, it sets that section to be true

public bool setGroup(Control ctrl)
    {
        isAControl = false;

        //set a section to true, so it will pull the html
        if (ctrl.ID.StartsWith("GenInfo_"))
        {
            GenInfo = true;
            lstControls.Add(ctrl.ID.Replace("GenInfo_", ""));
            isAControl = true;
            return isAControl;
        }

这是我的代码的一小段代码,我只想检查某些控件(以加快处理速度),并且遍历每个控件,因为每个控件都有获取值的不同方法(文本框将使用.text,其中dropdownlist将使用.selectedValue)

here is a small snippet of my code I only want to check for certain controls(to speed things up) and I go through each control as each control has a different way to get the value (textbox would use .text where dropdownlist would use .selectedValue)

if(control is TextBox || control is DropDownList || control is RadioButton || control is RadioButtonList 
                || control is CheckBox || control is CheckBoxList)
                {
                    if (control is TextBox)
                    {
                        TextBox txt = control as TextBox;
                        if (txt.Text != "" && txt.Text != "YYYY/MM/DD")
                        {
                            setGroup(control);
                            if (isAControl)
                            {
                                string controlNoGroup = lstControls.Last();
                                strHtml = strHtml.Replace("@" + (controlNoGroup.ToString()) + "@", txt.Text);
                            }
                        }
                    }

这篇关于更好的方法?查找ASP.NET控件,找到其ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆