2016-01-21 110 views
2

在我使用C#編寫的asp.net項目中,我需要從後面的代碼調用JQuery函數。 下面是功能包括所需的腳本:從服務器調用JQuery函數(asp.net)

<link rel="stylesheet" href="/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" /> 

    function showFancy(url, title, width, height) { 
     //alert(url); 
     $(".fancyBox").fancybox({ 
      'width': width, 
      'height': height, 
      'autoScale': true, 
      'transitionIn': 'elastic', 
      'transitionOut': 'none', 
      'type': 'iframe', 
      'overlayColor': '#000000', 
      'overlayOpacity': 0.7, 
      'position': 'fixed', 
      'scrolling': 'yes', 
      'modal': false, 
      'target': "_parent", 

      "onClosed": function() { 
       window.location = window.location; 
      } 

     }); 
     $('.fancyBox').attr({ href: url, title: title }); 
     $('.fancyBox').click(); 

    } 

的方式和我從aspx.cs撥打:

  protected void Button1_Click(object sender, EventArgs e) 
     { 
ClientScript.RegisterClientScriptBlock(this.GetType(), "Script", "showFancy('NewForm.aspx?ordernumber2=' + $('#TxtContractNumber').val(), 'Send', '55%', '65%');", true); 
      } 

,當我使用的輸入按鈕調用來自客戶端的功能,但是當它工作正常我從服務器端調用它不會調用newform.aspx(當我從showFancy函數取消註釋「alert(url);」時,警報有效)。我相信我稱之爲「新形式」的方式有一個問題。預先感謝您的時間和答案。

回答

1

如果您想要通過點擊按鈕Button1來執行此JavaScript,您需要將其添加爲代碼後面的onclick屬性。您可以在Page_LoadOnPreRender上添加此屬性。

Button1.Attributes.Add("onclick", "showFancy(...); return true;") 

,則返回true在年底將調用按鈕的服務器端(Button1_Click)的JavaScript後,如果您需要做一些服務器操作。

+0

謝謝'mybirthname'。使用您的建議並將以下代碼添加到aspx.cs文件的page_load中:Button1.Attributes.Add(「onclick」,「showFancy('NewForm.aspx?ordernumber2 ='+ $('#TxtContractNumber')。val(), '發送','55%','65%');返回true;「); ,現在它以fancybox格式(就像彈出窗口)打開newform.aspx頁面,但幾秒鐘後消失,之後它不起作用。你認爲問題是什麼? –

+0

如果你想讓fancybox保持打開狀態,最後放置返回false,不會返回true。返回false將不會執行按鈕的服務器端調用,並且頁面不會進行回發。 – mybirthname

+0

@SalmanBz現在可以嗎?不正確。 – mybirthname