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

前端 未结 5 1483
我寻月下人不归
我寻月下人不归 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:03

    Is Viewstate turned on?

    Edit: Perhaps you should reconsider overriding the rendering function

      protected override void RenderContents(HtmlTextWriter output)
        {
            ddlCountries.RenderControl(output);
            ddlStates.RenderControl(output);
        }
    

    and instead add the dropdownlists to the control and render the control using the default RenderContents.

    Edit: See the answer from Dennis which I alluded to in my previous comment:

    Controls.Add ( ddlCountries );
    Controls.Add ( ddlStates );
    
    0 讨论(0)
  • 2020-12-30 08:13

    I can't see that you're adding these controls to the control hierarchy. Try:

    Controls.Add ( ddlCountries );
    Controls.Add ( ddlStates );
    

    Events won't be invoked unless the control is part of the control hierarchy.

    0 讨论(0)
  • 2020-12-30 08:13

    I had the same problem but got round it by setting AutoPostBack to true and in an update panel set the trigger to the dropdownlist control id and event name to SelectedIndexChanged e.g.

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" enableViewState="true">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ddl1" EventName="SelectedIndexChanged" />
            </Triggers>
            <ContentTemplate>
                <asp:DropDownList ID="ddl1" runat="server" ClientIDMode="Static" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" AutoPostBack="true" ViewStateMode="Enabled">
                    <asp:ListItem Text="--Please select a item--" Value="0" />
                </asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 08:19

    You need to set AutoPostBack to true for the Country DropDownList.

    protected override void OnLoad(EventArgs e)
    {
        // base stuff
    
        ddlCountries.AutoPostBack = true;
    
        // other stuff
    }
    

    Edit

    I missed that you had done this. In that case you need to check that ViewState is enabled.

    0 讨论(0)
提交回复
热议问题