Disabling a button if textbox is empty
asp.net, c#
Solution
`System.String` provides a pair of convenient functions called `IsNullOrEmpty` and `IsNullOrWhiteSpace` which you can use for testing for all kinds of strings that look empty to end users:
if (string.IsNullOrWhiteSpace(YourName.Text)) {
SubmitButton.Enabled = false; // <<== No double-quotes around false
} else {
// Don't forget to re-enable the button
SubmitButton.Enabled = true;
}
This would disable the button even for string composed entirely of blank characters, which makes sense when you validate a name.
The above is identical to
SubmitButton.Enabled = !string.IsNullOrWhiteSpace(YourName.Text);
which shorter than a version with an `if`.
Problem
I want to find out if I can create a real time event that would check for value of a textbox. What I mean is if the textbox is empty the button wouldnt be clickable. I am thinking is there anyway apart from validation to do this in code? So far im stuck here: ``` if (YourName.Text = null) { SubmitButton.Enabled = "False"; } ``` Where Yourname is the textbox and SubmitButton is the button :)