1
foreach (Control ctrl in Page.Controls) 
    {
        if (ctrl is TextBox)
        {
            if (((TextBox)(ctrl)).Text == "")
            {  
               helpCalss.MessageBox("Please fill the empty fields", this);
                    return;  
            }  
        }  
    }  

I'm using asp.net and i have an inserting page with texboxes and i need to check if the texboxes in the page are empty and if so i need to show a message box with the empty textbox

Chris S
  • 64,770
  • 52
  • 221
  • 239

3 Answers3

0

Here is a good article on it, but below is a modified version that collects controls by a given property

public List<Control> ListControlCollections(Page page, string propertyName)
{
    List<Control> controlList = new List<Control>();
    AddControls(page.Form.Controls, controlList, propertyName);
    return controlList;
}

private void AddControls(ControlCollection controlCollection, List<Control> controlList, string propertyName)
{
    foreach (Control c in controlCollection) {
        PropertyInfo propertyToFind = c.GetType().GetProperty(propertyName);

        if (propertyToFind != null) {
            controlList.Add(c);
        }

        if (c.HasControls()) {
            AddControls(c.Controls, controlList, propertyName);
        }
    }
}

To use it:

List<Control> controlList = ListControlCollections("Text");
for (i=0; i < controlList.length; i++)
{
   if (controlList[i].Text == string.empty)
   {
      // Do your logic here
   }
   else  
   {
      // Do your logic here
   }
}
David East
  • 31,526
  • 6
  • 67
  • 82
  • 1
    @AVD i already tried with javascript but the problem is that i have a master page and content pages so the id is different and the javascript code didn't work when i used clientid proerty – Omnia Elshazly Jun 28 '12 at 14:03
  • i need to loop for all the controls in the page and if it's type is textbox i need to check if it is empty or not and if it is empty a message box should appear with the empty textbox and rollback to put text in the empty one – Omnia Elshazly Jun 28 '12 at 14:06
  • thanks for your code but how it's going to help me in this issue ? – Omnia Elshazly Jun 28 '12 at 14:08
  • I updated the answer. The above code will collect all of the controls on the page. Once you have all the controls you can then loop through them and check all of their values. It is extremely useful in your case. – David East Jun 28 '12 at 14:10
  • @OmniaElshazly - You may set [Control.ClientIdMode=Static](http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx) and Control.ClientID to reference server control's id in JavaScript/jQuery. – KV Prajapati Jun 28 '12 at 14:16
0

I guess you can try this recursive function to get all the textboxes on the page.

    /// <summary>
    /// Find TextBoxes Recursively in the User control's control collection
    /// </summary>
    /// <param name="controls"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    private void FindControlRecursiveByType(ControlCollection controls, ref List<TextBox> OutputList)
    {
        foreach (WebControl control in controls.OfType<WebControl>())
        {
            if (control is TextBox)
                OutputList.Add(control as TextBox);
            if (control.Controls.Count > 0)
                FindControlRecursiveByType(control.Controls, ref OutputList);
        }
    }

OutputList will contain all the textboxes and then you can check them for empty conditions

DotNetUser
  • 6,494
  • 1
  • 25
  • 27
0

Your approach is wrong, as people have pointed out - unless you are dynamically adding controls to the page then you should be performing validation via validators.

Here's a snippet of how to do it anyway:

private void SearchControls(Control controlSearch)
{
    foreach (Control control in controlSearch.Controls)
    {
        if (control != null)
        {
            if (control.Controls != null & control.Controls.Count > 0)
            {
                SearchControls(control, form);
            }

            TextBox textbox = control as TextBox;
            if (textbox != null && string.IsNullOrEmpty(textbox.Text))
            {

            }
        }
    }
}

Use SearchControls(this) in the page to start the search.

Chris S
  • 64,770
  • 52
  • 221
  • 239