简体   繁体   English

有没有办法用javascript清除列表视图?

[英]Is there any way to clear listview with javascript?

<asp:ListView ID="lvZipcodeFinder"></asp:ListView>

I am using my listview at the modal popup.我在模态弹出窗口中使用我的列表视图。 So, If modalpopup close and open, listview has to be cleared.... so Is there any way to clear the listview with javascript?因此,如果 modalpopup 关闭并打开,则必须清除 listview.... 那么有没有办法用 javascript 清除 listview? It must be cleared with javascript.... please, Help me..它必须用javascript清除....请帮帮我..

Yes you can do that: (on Server Side remove the actual items from DOM )是的,您可以这样做:(在服务器端从 DOM 中删除实际项目

Just add a hidden button to your .aspx page:只需向您的 .aspx 页面添加一个隐藏按钮:

 <asp:Button ID="btnClearListView" runat="server" Text="Button" style="display:none;" OnClick="btnHidden_Click" ClientIDMode="Static" />

and in your javascript when you call close modal you just use this code as well.在您的 javascript 中,当您调用 close modal 时,您也只需使用此代码。

 document.getElementById('btnClearListView').click();

and in your code behind for that button you will have something like this:在该按钮的后面代码中,您将有如下内容:

 protected void btnClearListView_Click(object sender, EventArgs e) { ListView1.Items.Clear(); }

You could do the same at the time of opening a modal-popup as well.您也可以在打开模态弹出窗口时执行相同的操作。

(on Client Side using jQuery) (在客户端使用 jQuery)

This is another way to do it but this would only disappear the control rather that removing the actual items from the listview.这是另一种方法,但这只会使控件消失,而不会从列表视图中删除实际项目。 you should know that if you require to remove a server control you must do it using server side coding.您应该知道,如果您需要删除服务器控件,则必须使用服务器端编码来完成。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $("#Submit1").click(function () {
            $("#lvZipcodeFinder_itemPlaceholderContainer").remove();
        });
    });
</script>

<input id="Submit1" type="button" value="Close Popup" />

Make sure the ListView id is match with the javascript code above, you need that _itemPlaceholderContainer afterwards though确保 ListView id 与上面的 javascript 代码匹配,但之后你需要 _itemPlaceholderContainer

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

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