Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Better way? Finding ASP.NET controls, finding their id

Tags:

c#

asp.net

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;
                    }
                }
            }
        }
    }
}      
like image 619
Spooks Avatar asked Jun 01 '10 18:06

Spooks


People also ask

What is ASP.NET find control?

The FindControl method can be used to access a control whose ID is not available at design time. The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page.

How do I find controls in C#?

Or you may find this textbox directly inside the grid view row (if the textbox id is unique in the page) as follows: TextBox txt = (TextBox)gr. FindControl("TextBoxID"); Now you can change the text of the control as : txt.

What is a control ID?

Control iD is currently the leader in the electronic time attendance device market. REP iDClass is certified by MTE and Inmetro and designed to serve companies of all sizes. Our biometric time attendance device provides security and reliability to businesses. Learn More.


2 Answers

Why not just use the Control.FindControl(string) method?

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");
      }
}

from: https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol

like image 62
Dan Avatar answered Oct 21 '22 08:10

Dan


It is hard to understand the logic behind your code, but I'm sure it can be written easier. For example you can do something like this:

DropDownBox box = FlattenHierachy(Page)
   .Where(c => c is DropDownList)
   .Cast<DropDownList>()
   .Where(d => d.SelectedIndex != -1)
   .FirstOrDefault();
if (box != null)
{
   if (box.ID.StartsWith("GenInfo_"))
   {
      GenInfo = true;
   }
   if (box.ID.StartsWith("EmpInfo_"))
   {
       EmpInfo = true;
   }
}

Obviously you can make this generic if you extract the lambda expression from the seconde Where call. So you could reuse it for different types. That's the solution which is as close to your code as possible, but I guess it would be a better idea to use a recursive method traversing the page and giving that method your predicates as lambda expressions.

like image 1
Achim Avatar answered Oct 21 '22 08:10

Achim