简体   繁体   English

即使使用autopostback = true,OnSelectedIndexChange也不会触发

[英]OnSelectedIndexChange not firing even with autopostback=true

Ok I'm a little stumped here. 好吧,我在这里有些困惑。 I have an asp:Dropdownlist on the page. 我在页面上有一个asp:Dropdownlist。 It gets populated via a web service call like this: 它是通过如下所示的Web服务调用填充的:

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. 现在页面正在回发,Page.Request.Params.Get(“ __ EVENTTARGET”)甚至说由于下拉列表而正在回发。 However, none of the code in my ddlMinorAssociation_SelectedIndexChanged function is being run. 但是,我的ddlMinorAssociation_SelectedIndexChanged函数中没有任何代码正在运行。 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. 它无法触发OnSelectedIndexChanged事件,因为其索引没有更改-它没有任何项目可以为其提供有意义的当前索引值。

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; 我认为问题与以下事实有关:页面加载时,下拉列表中没有元素,并且ViewState没有选定项目的任何信息; therefore, when it posts back, it determines that there hasn't been any IndexChanged event to fire. 因此,当它发回时,它确定没有要触发的IndexChanged事件。

The fact that it posts back is just because the Autoposback property is set to true which basically fires a normal form submit. 它回发的事实仅仅是因为Autoposback属性设置为true,这实际上会触发正常的表单提交。

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. 如果您希望这样做,请将AutoPostback属性更改为false,然后将代码挂钩到onchange事件,然后向您需要在服务器端执行的任何方法激发另一个Ajax请求,并在客户端绑定数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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