ASP.NET / C#: DropDownList SelectedIndexChanged in server control not firing

前端 未结 5 1487
我寻月下人不归
我寻月下人不归 2020-12-30 07:39

I\'m creating a server control that basically binds two dropdown lists, one for country and one for state, and updates the state dropdown on the country\'s selectedindexchan

5条回答
  •  北海茫月
    2020-12-30 08:17

    First, I would like to clarify something. Is this a post back (trip back to server) never occur, or is it the post back occurs, but it never gets into the ddlCountry_SelectedIndexChanged event handler?

    I am not sure which case you are having, but if it is the second case, I can offer some suggestion. If it is the first case, then the following is FYI.

    For the second case (event handler never fires even though request made), you may want to try the following suggestions:

    1. Query the Request.Params[ddlCountries.UniqueID] and see if it has value. If it has, manually fire the event handler.
    2. As long as view state is on, only bind the list data when it is not a post back.
    3. If view state has to be off, then put the list data bind in OnInit instead of OnLoad.

    Beware that when calling Control.DataBind(), view state and post back information would no longer be available from the control. In the case of view state is on, between post back, values of the DropDownList would be kept intact (the list does not to be rebound). If you issue another DataBind in OnLoad, it would clear out its view state data, and the SelectedIndexChanged event would never be fired.

    In the case of view state is turned off, you have no choice but to rebind the list every time. When a post back occurs, there are internal ASP.NET calls to populate the value from Request.Params to the appropriate controls, and I suspect happen at the time between OnInit and OnLoad. In this case, restoring the list values in OnInit will enable the system to fire events correctly.

    Thanks for your time reading this, and welcome everyone to correct if I am wrong.

提交回复
热议问题