Valley
Valley

Reputation: 41

Disable textboxes based on radio button selection

I have three radio buttons and five text boxes in my web page. I want to disable text boxes based on the radio box selection. This code is working fine but when I check a different radio button the previous gray textbox is not turning white. Please correct me I have tried several ways it did not work.

 protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
        {
            txtName.Enabled = false;
            txtTitle.Enabled = false;
            txtOrganization.Enabled = false;
            txtPhone.Enabled = false;
            txtEmail.Enabled = false;

        }
        if (rdPhone.Checked == true)
        {
            txtName.Enabled = true;
            txtName.Focus();
            txtTitle.Enabled = true;
            txtOrganization.Enabled = true;
            txtPhone.Enabled = true;
            txtEmail.Enabled = false;
            txtEmail.BackColor = System.Drawing.Color.LightGray;
        }
        if (rdEmail.Checked == true)
        {
            txtName.Enabled = true;
            txtName.Focus();
            txtTitle.Enabled = true;
            txtOrganization.Enabled = true;
            txtPhone.Enabled = false;
            txtPhone.BackColor = System.Drawing.Color.LightGray;
            txtEmail.Enabled = true;
        }

        if (rdDoNotContact.Checked == true)
        {
            txtName.Enabled = false;
            txtName.BackColor = System.Drawing.Color.LightGray;
            txtTitle.Enabled = false;
            txtTitle.BackColor = System.Drawing.Color.LightGray;
            txtOrganization.Enabled = false;
            txtOrganization.BackColor = System.Drawing.Color.LightGray;
            txtPhone.Enabled = false;
            txtPhone.BackColor = System.Drawing.Color.LightGray;
            txtEmail.Enabled = false;
            txtEmail.BackColor = System.Drawing.Color.LightGray;

        }
    }

Upvotes: 0

Views: 5199

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460098

You are changing the color to lightgray if you disable the TextBox but you are not changing it back when you enable it:

txtEmail.Enabled = true;
txtEmail.BackColor = System.Drawing.Color.White;

Upvotes: 3

Related Questions