eventHandler not working

  • Thread starter Thread starter lauralucas
  • Start date Start date
L

lauralucas

Hello

I have this code in a newly created web form:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
ArrayList values = new ArrayList();
values.Add("hi");
values.Add("hello");
this.cbBranches.DataSource = values;
this.cbBranches.DataBind();
this.cbBranches.SelectedIndexChanged += new
System.EventHandler(this.cbBranches_SelectedIndexChanged);
}

even so, the method cbBranches_SelectedIndexChanged never gets called
what can I do?
 
As you probably already know, the Controls in a WebForm are re-created with
each PostBack. When you call DataBind() on a List Control, it re-initializes
the Control's List, and cancels out any Selected Index changes. What you
want to do is only DataBind when you are initially filling the list. It will
retain the list across PostBacks.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
 
The OP posted to a winform group and is refering to combo boxes not Drop
down lists, so I assume the webform comments are moot here.

It appears that the event wire-up is fine (as long as you have a cbBranches_SelectedIndexChanged(object
sender, EventArgs e) method). You could be running into an issue with the
combo box itself. For example, if you type the value manually, selectedIndexChanged
won't fire. You need to catch this with an handler for TextChanged as well.
If you clear out the value, SelectedIndexChanged won't fire (even though
it changes from some value to -1). SelectedIndexChanged only fires when you
select a value with the mouse or up/down arrows. Otherwise, you need other
handlers to take care of the events. All of this assumes .NET 2.0. There
are additional issues with the 1.x combo box.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
The OP posted to a winform group and is refering to combo boxes not Drop
down lists, so I assume the webform comments are moot here.

Considering the facts that he (1) said "... a newly created webform", (2)
posted code for a method called "Page_Load" which has the exact same
signature as a WebForm, and (3) has a comment in that code that says "Put
user code to initialize the page here", I think it's a good bet he's working
on an ASP.Net application that uses a WebForm, regardless of what newsgroup
he posted to.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
 
Back
Top