2013-04-24 36 views
0

我有一個listview,我需要添加一個彈出窗口打開右鍵單擊事件。目前我已經用jQuery實現了它。但是頁面的初始加載,右鍵不起作用。但是,如果我刷新頁面,然後右鍵單擊正在工作。我已經查看html源代碼,並且可以正確加載所有標記和jquery變量。asp.net listview item的Rigth點擊事件

這是我的標記。

<asp:ListView ID="lvKeywords" OnItemDataBound="lvKeywords_ItemDataBound" OnItemDeleting="lvKeywords_ItemDeleting" 
      DataKeyNames="Key" runat="server" OnItemUpdating="lvKeywords_OnItemUpdating" > 
    <ItemTemplate> 

     <div id="<%#Eval("Key") %>" class="fsKeywordItem"> 
      <asp:Literal ID="ltrlKeyword" runat="server"></asp:Literal>     
     </div> 

     <%--this is the popup--%> 
     <div class="smartPopUp" id="<%#Eval("Suggession") %>" style="display: none;"> 
      <%-- there are some html controls here --%> 
     </div> 

     <script type="text/javascript"> 

      $(document).ready(function() { 
       $("#" + '<%#Eval("Key") %>').mousedown(function (event) { 
        switch (event.which) { 
          case 3: 
           event.preventDefault(); 
           showPopup('<%#Eval("Suggession") %>'); 
          } 
       }); 

       $("#" + '<%#Eval("Key") %>').bind("contextmenu", function (e) { 
        e.preventDefault(); 
       }); 
      }); 
     </script> 
    </ItemTemplate> 
</asp:ListView> 

<script type="text/javascript"> 
    function showPopup(divId) { 
     $("#" + divId).css("display", "block"); 
    } 
</script> 

有關此問題的任何想法?

+0

檢查它 http://www.codeproject.com/Articles/63902/Right-Click-Menu-using-JQuery-ASP-NET-using-C – 2013-04-24 04:24:06

回答

0

我認爲這是因爲document.ready()。所以刪除ListView中的jquery部分並使用javascript來完成它。

所以,我給你一個listview的例子。

oncontextmenu='showPopup("popupDivId"); return false;' 

給你的DIV添加如下

<div id="<%#Eval("Key") %>" class="fsKeywordItem" oncontextmenu='showPopup("popupDivId"); return false;'> 
     <asp:Literal ID="ltrlKeyword" runat="server"></asp:Literal>     
    </div> 

    <%--this is the popup--%> 
    <div class="smartPopUp" id="<%#Eval("Suggession") %>" style="display: none;"> 
     <%-- there are some html controls here --%> 
    </div> 

將在這裏發生什麼事,oncontextmenu將火分辯點擊事件,然後我們有我們的調用函數。那麼「返回false」將停止右鍵單擊事件的默認行爲。這意味着它會停止瀏覽器的默認右鍵菜單。

希望你可以通過使用oncontextmenu找到解決方案。

:)