javascript - 在 asp.net 中使用 updatePanel 后,DropDownList 丢失其属性

标签 javascript c# asp.net

我有下面的代码在 asp.net 中有智能下拉列表,在那里有搜索属性

<script type="text/javascript"> $(".chzn-select").chosen(); $(".chzn-select-deselect").chosen({ allow_single_deselect: true }); </script>

问题是每当我以这种方式使用 updatepanel 时:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <ContentTemplate>
        <fieldset>
        </fieldset>
    </ContentTemplate>
</asp:UpdatePanel>  

在那之后:它失去了它的属性并作为正常的下拉列表返回。知道如何解决这个问题并使更新面板不重新创建下拉列表到它以前的属性吗?!

最佳答案

您需要在 UpdatePanel 更新后再次执行该脚本。由于 DOM 仍在变化并且浏览器丢失了脚本绑定(bind)。为此,您可以使用 PageRequestManager

<script type="text/javascript">

    var prm = Sys.WebForms.PageRequestManager.getInstance();

    //for after an updatepanel update
    prm.add_endRequest(function () {
        buildDropDownList();
    });

    //for normal page load
    buildDropDownList();

    function buildDropDownList() {
        $(".chzn-select").chosen();
        $(".chzn-select-deselect").chosen({ allow_single_deselect: true });
    }
</script>

关于javascript - 在 asp.net 中使用 updatePanel 后,DropDownList 丢失其属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51584624/

相关文章:

javascript - “return deferred.promise(this)”返回未定义

c# - 为什么我们需要模拟框架?

c# - Umbraco 应用程序 BeginRequest 从未触发

c# - 将 IQueryable Where 与具有多个参数的表达式一起使用

c# - Xamarin Forms 没有为 UITest 设置 StyleId

c# - 将下拉菜单与年份绑定(bind)

javascript - 当其他文本框被填充时填充文本框

javascript - .net core Razor 页面与 JQuery AJAX

javascript - 如何使用扩展运算符向对象添加属性而不是覆盖它?

javascript - 更改 HTML 占位符元素的格式