我有一個asp:listview
控件,它在後面的代碼中填充來自數據綁定數據表的數據以創建動態列表。使用該列表,我有一個鏈接按鈕,我希望綁定到JavaScript函數以隱藏相應的li
元素(如果點擊)。這是我的HTML:如何在使用JavaScript的asp列表視圖控件中隱藏linkbutton點擊列表元素
<asp:ListView ID="lstAIInsureds" runat="server">
<LayoutTemplate>
<ul class="collection">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="collection-item" id="liRemoveAI">
<div>
<%#Eval("Response").ToString().Replace("|",", ")%>
<asp:LinkButton ID="lbRemoveAI" class="material-icons" ClientIDMode="Static" OnClientClick="return RemoveAI();" runat="server">remove_circle</asp:LinkButton>
</div>
</li>
</ItemTemplate>
<EmptyDataTemplate>
<p>Nothing here.</p>
</EmptyDataTemplate>
</asp:ListView>
和我後面的代碼被綁定像這樣:
this.lstAIInsureds.DataSource = dt;
this.lstAIInsureds.DataBind();
這是我目前的JS,但當然在縫隙for循環而這正是我需要完成:
function RemoveAI() {
var olsRemoveAI = document.getElementById("liRemoveAI"); //this is the ls that is to be hidden
var obtRemoveAI = document.getElementById("lbRemoveAI"); //this is the linkbutton that is clicked to hide the parent ls element
for (i = 0; i < olsRemoveAI.???.length; i++) {
//if this button is contained in the parent li, mark the li as hidden:
olsRemoveAI.style.visibility = "hidden";
olsRemoveAI.style.display = "none";
//else:
olsRemoveAI.style.visibility = "visible";
olsRemoveAI.style.display = "block";
}
return false;
}
這是可行的嗎?非常感謝援助!
但我需要一個解決方案,在沒有頁面加載的情況下處理JS。如果需要移除runat服務器標籤來解決此問題,那很好。我的目標是通過單擊刪除按鈕而不回發來摺疊此控件。 –
正如此處按鈕文檔中所述(https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button(v=vs.110).aspx),您可以使用屬性OnClientClick =「string」其中string是JS中函數的名稱。 –