2013-04-24 80 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找到解决方案。

:)