Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
How Can i validate many text boxes at a time by set errorprovider on those textboxes.
i want to use it on butten click event.
Posted 31-Jan-13 22:47pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi... You can do it by this way...
 
public bool CheckTextBoxNull(TextBox txt)
       {
           if (txt.Text.Trim() == "" || txt.Text==string.Empty())
           {
               txt.BackColor = Color.FromArgb(255, 192, 192);
               txt.Focus();
               return false;
           }
           else
           {
               return true;
           }
       }
 

private bool ValidateFn()
        {
           #region Object Validate Function
 
           if (CheckTextBoxNull(textbox1) == false) { return false; }
 
           if (CheckTextBoxNull(textbox2) == false) { return false; }
           return true;
        }
 
private void btn_Save_Click(object sender, EventArgs e)
        {
            #region Save Funtion
            if (ValidateFn() == true)
            {
                // you Code here...
            #endregion
        }
         
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

StringBuilder errorMessage = new StringBuilder();
foreach(Control c in Page.Controls) 
{ 
    if (c is TextBox) 
    { 
        if(ur conditions)
        {
          errorMessage.Append(c.ID.ToString()+"Has Error");
        }        
    } 
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can create method given below for all similar kind of controls. You can call this method by passing array of control i.e in your case you can call like given below (pass all textboxs which has to validated)
 
TextBox[] listTextBox = {textBox1,textBox2};
 
public bool CheckTextBoxNull(TextBox[] listTextBox)
   {
       for (int i = 0; i <= listTextBox.Length - 1; i++)
       {
           if (listTextBox[i].Text.Trim().Length == 0)
           {
               listTextBox[i].BackColor = System.Drawing.Color.FromArgb(255, 192, 192);
               listTextBox[i].Focus();
               return false;
           }
       }
       return true;
   }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 751
1 George Jonsson 330
2 Garth J Lancaster 290
3 Sergey Alexandrovich Kryukov 274
4 BillWoodruff 252
0 Sergey Alexandrovich Kryukov 10,879
1 OriginalGriff 5,555
2 Maciej Los 5,534
3 CPallini 3,819
4 BillWoodruff 3,594


Advertise | Privacy | Mobile
Web03 | 2.8.141030.1 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100