I created a web form.
I added a:
DropDownList
TextBox
Label.
Here is my code. It works for me.
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack )
{
LoadDDL();
}
}
private void LoadDDL()
{
ListItem li = new ListItem("--Select--", "0");
this.DropDownList1.Items.Add (li);
li = new ListItem("Enable Me", "1");
this.DropDownList1.Items.Add (li);
li = new ListItem("Disable Me", "2");
this.DropDownList1.Items.Add (li);
}
private void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if (this.DropDownList1.SelectedItem.Value == "2")
{
this.TextBox1.Enabled = false;
this.Label1.Text = "false";
}
else
{
this.TextBox1.Enabled = true;
this.Label1.Text = "true";
}
}
Before you do shortening ... like
this.TextBox1.Enabled = this.DropDownList1.SelectedValue == "1";
I'd break it down into parts first, since your new to the controls.
<ke***************@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
I want to conditionally enable a textbox based on the selection of a
dropdown.
I have 2 entries in the dropdown: Nothing (value 99), Enabled (value
1), Disabled (value 0). Autopostback on the dropdown and textbox are
set to true.
The code in Page_Load is ...
private void Page_Load(object sender, System.EventArgs e){
this.TextBox1.Enabled = this.DropDownList1.SelectedValue == "1";
}
This works fine, for one change in the dropdown selection. After I
change the value a second time it stops working.
My background is windows programming, so I am not entirely comfortable
with this. I searched the group and the help pages to see what the
problem could be, but didn't see anything. Any help is appreciated.
- K.