【问题标题】:How to prevent a dropdownlist from postback or updating inside an updatepanel when button is clicked单击按钮时如何防止下拉列表在更新面板中回发或更新
【发布时间】:2018-01-08 05:03:39
【问题描述】:

单击按钮时如何防止下拉列表在更新面板中回发或更新。 我这样做是因为我有 java 脚本来使 ddl 成为可搜索的下拉列表。我在更新面板中有一些标签、文本框、网格视图和下拉列表。当我单击查看按钮时,下拉列表会变回正常下拉列表,而不是可搜索的下拉列表。

<div>
    <asp:DropDownList ID="ddlSector" runat="server" Width="150px"></asp:DropDownList>
    <asp:DropDownList ID="ddlCity" runat="server" Width="150px"></asp:DropDownList>
    <asp:TextBox ID="txtStdID" runat="server" Width="92px" MaxLength="5"></asp:TextBox>
    <asp:Button ID="btnView" runat="server" Text="View" onclick="btnView_Click" AccessKey="V"/>
<table>
    <tr><td><b>Qualification</b></td><td><b>Exemption Type</b></td></tr>
    <tr>              
    <td><asp:DropDownList ID="ddlQualification" class="chzn-select" runat="server" Width="225px" onselectedindexchanged="ddlQualification_SelectedIndexChanged" 
    AutoPostBack="false"></asp:DropDownList></td>
    <td>
    <asp:DropDownList ID="ddlExemType" runat="server" Width="225px">
    </asp:DropDownList></td>        
    </tr>
</table>
</div>
<script src="../Searchable DDL/jquery.min.js"type="text/javascript"></script>  
<script src="../Searchable DDL/chosen.jquery.js"type="text/javascript"></script>  
<script type="text/javascript">
     $(".chzn-select").chosen(); 
 $(".chzn-select-deselect").chosen({ allow_single_deselect: true });</script>

【问题讨论】:

    标签: c# asp.net visual-studio webforms


    【解决方案1】:

    即使没有完整的 PostBack,UpdatePanel 中的所有内容仍会刷新,因此 DOM 会丢失使用 jquery 修改的元素。

    您还需要在异步 PostBack 之后调用创建可搜索下拉列表的函数。

    <script type="text/javascript">
        $(document).ready(function () {
            createSearchDropDown();
        });
    
        var prm = Sys.WebForms.PageRequestManager.getInstance();
    
        prm.add_endRequest(function () {
            createSearchDropDown();
        });
    
        function createSearchDropDown() {
            $(".chzn-select").chosen(); 
            $(".chzn-select-deselect").chosen({ allow_single_deselect: true });
        }
    </script>
    

    【讨论】:

    • 我已经上传了我的java脚本代码,请检查一下。我仍然无法解决问题。\
    【解决方案2】:

    在 javascript 的 pageLoad 事件中编写你的 javascript 函数。

    它将始终在回发以及部分回发中执行。

    为您的函数查找以下代码

     function pageLoad() {
                 //This will ensure your code will run all the time
                 CreateDropdown();
                 InitializeDatePickers();
                 AssignPlugins();
    
            }
    

    【讨论】:

    • 这是我的 javasrcript 代码。我如何在页面加载事件中调用它? &lt;script src="../Searchable DDL/jquery.min.js"type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../Searchable DDL/chosen.jquery.js"type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(".chzn-select").chosen(); $(".chzn-select-deselect").chosen({ allow_single_deselect: true });&lt;/script&gt;
    • 你可以试试下面的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多