Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to check all textbox on click btncave if there is no empty text box do something else message error that you have to need to fill empty textbox frist
i try this code

C#
foreach (Control c in this.Controls)
{
    if (c is TextBox)
    {
        TextBox textBox = c as TextBox;
        if (textBox.Text == string.Empty)
        {
            textbox.text="please fill your empty textbox ";
        }
       
      else
         {
            // if my textbox all are fill i put my code to save data in datebase here
        }
    }
 }


i try this code but it do nothing ????
Posted
Updated 28-May-21 21:34pm

Use Required field validators. In fact it reduces the loading time and its need very samll validating time when compared to validate it in the code behind.

When all required fields satisfied execute your code in the submit button click event

To use required field validator refer this
http://asp.net-tutorials.com/validation/required-field-validator/[^]

http://asp-net-example.blogspot.in/2009/02/aspnet-requiredfieldvalidator-example.html[^]

Hope it helps.
 
Share this answer
 
Quote:
How to check All text boxes are empty or Not

string EmptyTextBoxes = string.Join(Environment.NewLine,
(
from T in this.Controls.OfType<textbox>()
where string.IsNullOrWhiteSpace(T.Text)
select T.Name
).ToArray()
);

if (EmptyTextBoxes.Length > 0)
{
MessageBox.Show("Please fill in\n" + EmptyTextBoxes);
}
 
Share this answer
 
v2
if you want to do a validation in one go of foreach then you need repeater or gridview control or datagrid or if you have all textbox straight i would suggest you to do a one by one
 
Share this answer
 
Comments
ost3z 19-Jul-13 9:00am    
thankx all for your advice i do by requried field vaidator
Dholakiya Ankit 20-Jul-13 2:41am    
see it accepted solutoin and rate 1 ha ha ha ha ha (:)

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