Reputation: 41
I have a form in which various textboxes are there and want to enable or disable the textbox if value entered in other textbox is greater than 0. Tried different solutions but no luck as of now. Lastly, I have tried this :
protected void TextBox7_TextChanged(object sender, EventArgs e)
{
if (TextBox7.Text.Trim().Length>0)
{
TextBox9.Enabled = true;
}
else
{
TextBox9.Enabled = false;
}
}
Upvotes: 2
Views: 1752
Reputation: 3261
Try this
string.IsNullOrEmpty(TextBox7.Text) ? TextBox9.Enabled = true : TextBox9.Enabled = false;
look for it being a string.IsNullOrEmpty(...), you could replace your original code with this if you wanted but the other line should work perfectly for you too
if (string.IsNullOrEmpty(TextBox7.Text))
{
TextBox9.Enabled = true;
}
else
{
TextBox9.Enabled = false;
}
Upvotes: 0
Reputation: 79
try this. first i think you need to disable textbox 9 first to get your requirement.
public Form1()
{
InitializeComponent();
textBox9.Enabled = false;
}
private void textBox7_TextChanged(object sender, EventArgs e)
{
if (Int32.Parse(textBox1.Text.Trim()) > 0)
{
textBox9.Enabled = true;
}
else
{
textBox9.Enabled = false;
}
}
Upvotes: 1
Reputation: 29026
Code looks fine except some irritating names(try to follow some naming conventions). Since it is web forms you have to make sure the following things to make it work:
Upvotes: 1