Server event not firing - need workaround

keb1965

Well-known member
Joined
Feb 9, 2009
Messages
103
Programming Experience
10+
I have a page that uses a third party combobox that does not fire the server side SelectedIndexChanged event if the user selects the top item in the list and the previous item was its default item. However, it does fire the client side event as it should. Changing controls is not an option as this is embedded into a huge project that would take months to refactor the code to work with a different control.

All other times it fires the event as expected.

While the developer works out a solution, I need to come up with a workaround that will fire the event from client-side.

I considered creating a WebMethod and just call it with the value and simply ignore the postback, but I need other client controls to be updated and to the best of my knowledge that isn't possible with a WebMethod unless I want to do a whole lot of client side code.
 
One solution would be to not set a default item, instead actually change to the item you want to be default on page load. I guess the issue is not really clear, is it a bug in the third party control, or is that the expected behavior? If so what is the vendor recommended way of dealing with this?

This is why I keep third party controls away unless I absolutely need to use one.
 
I guess I'll bore you with the details ;-)

The control selections are populated from preset values gathered from a database. The selected item is the matching value in a set of other records. Lets say the combobox is to select a person's age range. There are several items populating the combobox but none are selected - yet. Now lets imagine that for the current record, age_range evaluates to null. Because the values of other controls are set according to the selected value of the combobox, setting it to the incorrect value results in other values being incorrect. Plus, to add more onto this, on postback, if a value is selected that is incorrect, it will corrupt the data. This needs to be a value that is not selected and is picked up by the server side code as being invalid. The default item is selected whenever there is no valid item that can be selected and the data is safe.

The vendor was only aware of the issue on Friay after I brought it to their attention. They have no workaround as of yet because they have still not acknowledged it is a bug. After providing them with code and the steps to recreate the behavior, I suspect they will do so today.

Their first response was this is the expected behavior, except their own code that they said works as expected also has the same problem. The problem with their assertion is the client side event fires, but the server side event does not. Of course this is only the case when the top item in the combobox is selected first when the default item is currently selected.

Anyway, I was thinking that because I don't need to fire any events except the SelectedIndexChanged event, I could just handle the postback in the codebehind and fire it manually each time the combobox initiates a postback. I can probably do that by using the client side event and calling __doPostBack from the client code as long as I set AutoPostBack to false. This way, in the code behind, if I get a postback that is initiated from the control, I can call the appropriate function to ensure the changes are applied.

Something like this in the code behind:
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Page.IsPostBack) Then
        If (Request.Form("__EVENTTARGET") = "MyComboBox") Then
            MyComboBox_SelectedIndexChanged(MyComboBox , New EventArgs)
        End If
    End If
End Sub

and something like this in the client code:
VB.NET:
<tpv:ComboBox ID="MyComboBox" AutoPostBack="false" OnClientSelectedIndexChanged="MyComboBox_SelectedIndexChanged" runat="server" />

<script type="text/javascript">
function MyComboBox_SelectedIndexChanged(sender, args){
    __doPostBack('MyComboBox', '');
}
</script>

Is this workable? Are there any issues I need to be aware of?
 
Back
Top