Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Hi... You can do it by this way...

C#
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
        }
 
Share this answer
 
C#
StringBuilder errorMessage = new StringBuilder();
foreach(Control c in Page.Controls) 
{ 
    if (c is TextBox) 
    { 
        if(ur conditions)
        {
          errorMessage.Append(c.ID.ToString()+"Has Error");
        }        
    } 
}
 
Share this answer
 
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)

C#
TextBox[] listTextBox = {textBox1,textBox2};


C#
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;
   }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900