简体   繁体   中英

OnSelectedIndexChange not firing even with autopostback=true

Ok I'm a little stumped here. I have an asp:Dropdownlist on the page. It gets populated via a web service call like this:

Ajax.GetSubDevelopments(developmentId, function (results) {
    var subDevelopments = $j("#<%=ddlMinorAssociation.ClientID %>");
    subDevelopments.empty();
    $j('<option />', { value: "-1", text: "Select a sub association" }).appendTo(subDevelopments);
        $j.each(results, function (index, value) {
            $j('<option />', { value: value.SubDevelopmentID, text: value.Name }).appendTo(subDevelopments);
        });
        subDevelopments.show();
    });

The actual control itself looks like this:

<asp:DropDownList ID="ddlMinorAssociation" 
   OnSelectedIndexChanged="ddlMinorAssociation_SelectedIndexChanged" 
   AutoPostBack="true" runat="server" CssClass="hidden">
</asp:DropDownList>

The intent is that when the user selects a subdevelopment, it will postback and then bind a datalist of results. Now the page is posting back, Page.Request.Params.Get("__EVENTTARGET") even says that it was posting back due to the drop down list. However, none of the code in my ddlMinorAssociation_SelectedIndexChanged function is being run. Here is that code:

protected void ddlMinorAssociation_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write("hi");
}

Can anyone offer some insight as to what is going on???

Server-side, you dropdown list does not have any items. It cannot fire OnSelectedIndexChanged event because its index has not changed - it has no items to give it a meaningful current index value.

I believe the issue is related to the fact that when the page loaded, there were no elements in the dropdown list and ViewState didn't have any information of selected items; therefore, when it posts back, it determines that there hasn't been any IndexChanged event to fire.

The fact that it posts back is just because the Autoposback property is set to true which basically fires a normal form submit.

If you want this to work, change the AutoPostback property to false and hook code to the onchange event instead, then fire another Ajax request to whatever method you need to execute on the server side and bind the data on the client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM