Autopostback Confusion.

  • Thread starter Thread starter kevin.j.hutchison
  • Start date Start date
K

kevin.j.hutchison

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.
 
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.
 
Thank you for the very fast feedback.

Using this code in a new form with autopostback = true for
DropDownList1, I get the same behavior that I had before.

The first time I change the selection in the dropdownlist, the enabled
state is set correctly. Subsequent changes to the dropdown do not
change the enabled state. I see no evidence that the event is firing a
second time.
 
Solution:

C:\WINNT\Microsoft.NET\Framework\v2.0.40607\aspnet_regiis.exe -u
C:\WINNT\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i
 
Back
Top