Atul Patil
Atul Patil

Reputation: 41

Enable or disable textbox on value changed in other textbox

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

Answers (3)

Simon Price
Simon Price

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

Didu
Didu

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

sujith karivelil
sujith karivelil

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:

  • AutoPostBack property of the corresponding control is set to true
  • It will trigger only when the control loses focus

Upvotes: 1

Related Questions