2011-11-20 100 views
1

我需要知道我怎麼能叫下面的Javascript在Asp.Net一個按鈕單擊事件中調用JavaScript的從Asp.Net代碼隱藏(不是函數)

<script type='text/javascript'> 
     $("#various3").fancybox({ 
      'width': '75%', 
      'height': '75%', 
      'autoScale': false, 
      'transitionIn': 'none', 
      'transitionOut': 'none', 
      'type': 'iframe' 
     }); 
</script> 

這是我的正常Asp.Net按鈕

<asp:Button ID="Button1" runat="server" Text="Button" /> 

回答

2

將代碼包裝在一個函數中,並調用該按鈕的客戶端單擊事件上的函數。

<script type='text/javascript'> 
    function createFancyBox() { 
    $("#various3").fancybox({ 
     'width': '75%', 
     'height': '75%', 
     'autoScale': false, 
     'transitionIn': 'none', 
     'transitionOut': 'none', 
     'type': 'iframe' 
    }); 
    // include this line to stop the button to call the server side event 
    return false; 
    } 
</script> 

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return createFancyBox();" /> 
+0

謝謝waqas它運作良好! – coder

+0

+1,但由於該函數將始終返回false,並且沒有任何事情會從單擊該按鈕後回傳;我擺脫了'asp:button'控件,只是有一個常規的'button'元素。 – Icarus

+0

@ Waqas - 對於按鈕它不能正常工作,如果我使用超鏈接,它運作良好,可能是什麼原因呢? – coder