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 CPallini 510
1 Sergey Alexandrovich Kryukov 428
2 George Jonsson 298
3 Pheonyx 218
4 OriginalGriff 200
0 OriginalGriff 4,813
1 CPallini 3,850
2 Sergey Alexandrovich Kryukov 3,224
3 George Jonsson 2,552
4 Gihan Liyanage 2,186


Advertise | Privacy | Mobile
Web04 | 2.8.140905.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