Adam S-Price
Adam S-Price

Reputation: 2018

ListBox doesn't fire OnSelectedIndexChanged event when SelectedIndex is changed

I have an asp.net page with a list box on it. Multiple event handlers subscribe to its OnSelectedIndexChanged event.

When I change the SelectedIndex programmatically none of the events get fired.

Now a hack for this is to call each event handler, but this has already caused bugs since people didn't know they had to do this when adding a new event handler.

I can do this in a Winforms app and even when SelectedIndex is changed in code the events fire. Has anyone seen this before?

Upvotes: 1

Views: 2161

Answers (2)

Song
Song

Reputation: 673

As a workaround, I change data in database and complete events releated, then reload data with JavaScript.

Scenes: ASPxGridView control dtgRepair delete row, at the same time, ASPxComboBox value changed programmatically.

Server:

protected void dtgRepair_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
{    
// update database here...
cmbMRType.SelectedIndex = 1;
dtgRepair.JSProperties["cpCallbackAction"] = "DeleteEntry";
}  

Client:

function dtgRepair_EndCallback(s,e) { 
    if(cmbMRType.GetSelectedIndex() == 2 && dtgRepair.cpCallbackAction == "DeleteEntry" ) 
        window.location.reload(true);
}

Upvotes: 0

DevExpress Team
DevExpress Team

Reputation: 11376

Take a look at the source code of the ListBox class and its base - ListControl. You will notice that the OnSelectedIndexChanged method is called from the RaisePostDataChangedEvent method. This means, that the SelectedIndexChanged event is only raised if the selected index was changed on the client side and the value stored in the ViewState does not equal data comes with the PostData. So, this event should not be raised if the SelectedIndex was changed in the server code.

Upvotes: 2

Related Questions