Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown list event never triggers

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
I'm using MS V C#.NET

And I can't get my dropdown list to do anything. I select an item and in the code that is autogenerated by MS I put down it should put that item in the a text box.

When I keep on selecting items it doesn't do this. However when I click on a button unrelated the dropdown list code gets run.

Any ideas?

Drop down code.

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.TextBox3.Text = this.DropDownList1.SelectedItem.Value.ToString();

}

button code
private void Button1_Click(object sender, System.EventArgs e)
{
//When the button is clicked we call the ContributionCheck method which checks all the contributions and takes the appropriate action
ContributionCheck();
this.TextBox1.Text = myConnection.State.ToString(); //"something here -->";
ContributionCheckChangeColour(GREEN);
}
 
It is for a Windows or a Web app? I've had that happen on a web app when I bound the list to a datasource. When I went through the rows of a data set and added them programatticaly to the list....the event would occur.
 
if it is a webapp, then you most likely need to set the autopostback property of the dropdownlist to true

This places an onChange event handler on the dropdownlist, which will trigger your postback
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Thanks link9 I haven't tried this yet, but I get the feeling you are right.

One thing that still concerns me is why the method is getting run when I click the button. The button has its own method and hasn't got any reference to the drop down menu.
 
If I had to take a stab (and that's all it is, mind you), I'd say that most likely, it's because the selectedIndex has actually changed.

Therefore, the event is raised whether or not it was raised directly by a postback on that particular object, or not, it's index is still different than what the previous viewstate shows that it was, and so the event handler fires.
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Well, I'm not sure how the event is actually raised.

So, if in one scenario, the page class goes through and compares previous saved state to current state, and sees that the selectedIndex has changed, then I could see why the event handler fires even though you clicked the button.

The button event handler would also fire in the case, though, which I would assume is unwanted behavior.

In any case, just set the autoPostBack property to true, and all this should be moot.
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Chrissirhc,

The event doesn't fire until the server is invoked. It works like this.

1) The clients dropdown list (or any other control) is changed
2) The client posts back to the server (at some point)
3) The server runs the code

As the drop down list is not invoking postback, the code is not fired until postback occurs (i.e. your button click)

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top