c# – 一个更好的方法?找到ASP.NET控件,找到他们的id

我有一个方法找到所有控件,遍历它们,确定它们是文本框,下拉列表等等.检索它们的ID名称,并根据ID名称它将设置一个布尔语句(因此我会知道如果表格的那一部分是完整的,并将通过电子邮件发送给某一群人)不幸的是,这是通过太多的if语句完成的,并且想知道我是否可以得到一些帮助使这个更易于管理
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;
                    }
                }
            }
        }
    }
}

解决方法

为什么不使用Control.FindControl(string)方法

来自:http://msdn.microsoft.com/en-us/library/486wc64h.aspx

private void Button1_Click(object sender,EventArgs MyEventArgs)
{
      // Find control on page.
      Control myControl1 = FindControl("TextBox2");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
         Response.Write("Parent of the text Box is : " + myControl2.ID);
      }
      else
      {
         Response.Write("Control not found");
      }
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...