2015-08-19 37 views
0

在我的Asp.Net(C#)網頁中,我從Jquery Ajax做了95%的工作。只有打印工作是從服務器端代碼發生的,因爲它需要重定向另一頁進行打印。這裏是我的打印按鈕的代碼是否有可能在asp.net C#中的靜態方法中使用RegisterClientScriptBlock?

protected void btnprint_Click(object sender, ImageClickEventArgs e) 
    { 
     string status = "Normal"; 
     string Id = txtReceiptNo.Text; 
     string BillType = "BillReceipt"; 
     string Url = "BillReceipt.aspx";   
     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ApprovalHistory", "window.open('BillReceiptReport.aspx?Id=" + Id + "&Status=" + status + "&BillType=" + BillType + "&Url=" + Url + "', '_blank');", true); 
    } 

當我點擊打印按鈕將其重定向與另一個選項卡中的一些價值觀的PrintPage。

但問題是,當我點擊打印按鈕回發發生和一切擾亂我的網頁,因爲如上所述,我正在做95%的工作使用JQuery的Ajax。

所以我決定從jQuery的阿賈克斯做100%的工作,並試圖調用內部靜態該打印功能,但的WebMethod我發現的RegisterClientScriptBlock沒有內部靜態方法工作。我試圖做這樣的事情......

[WebMethod] 
    public static void PrintReport() 
    { 
     string status = "Normal"; 
     string Id = "40"; 
     string BillType = "BillReceipt"; 
     string Url = "BillReceipt.aspx"; 
     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ApprovalHistory", "window.open('BillReceiptReport.aspx?Id=" + Id + "&Status=" + status + "&BillType=" + BillType + "&Url=" + Url + "', '_blank');", true); 
    } 

請幫我傢伙....

+0

只是嘗試通過添加此行'page.ClientScript.RegisterStartupScript(「ApprovalHistory」,「」);' –

+0

您好@Satindersingh感謝您的評論,但'page.ClientScript'是不是在靜態方法工作 –

回答

0

您正在使用this(非靜態)你的IE在PrintReport靜態方法裏面( )方法

關鍵字'this'返回對包含它的類的當前實例的引用。靜態方法(或任何靜態成員)不屬於特定實例。它們存在而不創建類的一個實例。

請嘗試使用下面的代碼:

[WebMethod] 
    public static void PrintReport() 
    { 
     string status = "Normal"; 
     string Id = "40"; 
     string BillType = "BillReceipt"; 
     string Url = "BillReceipt.aspx"; 
     if (HttpContext.Current.CurrentHandler is Page) 
     { 
      Page page = (Page)HttpContext.Current.CurrentHandler; 

      if (ScriptManager.GetCurrent(page) != null) 
      { 
       ScriptManager.RegisterStartupScript(page, typeof(Page), "ApprovalHistory", "window.open('BillReceiptReport.aspx?Id=" + Id + "&Status=" + status + "&BillType=" + BillType + "&Url=" + Url + "', '_blank');", true); 
      } 
      else 
      { 
       page.ClientScript.RegisterStartupScript(typeof(Page), "ApprovalHistory", "window.open('BillReceiptReport.aspx?Id=" + Id + "&Status=" + status + "&BillType=" + BillType + "&Url=" + Url + "', '_blank');", true); 
      } 
     } 
    } 
+0

您好@Varun 感謝您的幫助,但它仍然無法正常工作 沒有顯示任何錯誤,但它沒有重定向到另一個頁面:( –

+0

你能請你發表你怎麼打電話PrintReport()? – Varun

1
ScriptManager.RegisterClientScriptBlock((Page)(HttpContext.Current.Handler), typeof(Page), "ApprovalHistory", "window.open('BillReceiptReport.aspx?Id=" + Id + "&Status=" + status + "&BillType=" + BillType + "&Url=" + Url + "', '_blank');", true); 
0

這不是你要去工作。您正在webmethod中註冊客戶端腳本塊。客戶端腳本塊在頁面加載時運行,而在Ajax調用頁面不會重新加載,因此它既不會給您任何錯誤,也不會運行您的腳本塊。你可以採取兩種方法來解決這個問題。

1:不要在您的web方法中註冊任何腳本塊,只需簡單地從您的web方法返回值(例如(Id,狀態,BillType,Url)並在ajax調用的成功塊內打開新頁面試圖從您的webmethod內部打開。

$.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      url: "Accounts.aspx/PrintReport", 
      data: JSON.stringify(Param), 
      type: "POST", 
      success: function (data) 
      { 
        var Id=data.d.Id; 
        var status=data.d.status; 
        var BillType=data.d.BillType; 
        var Url=data.d.Url; 
        var win=window.open("BillReceiptReport.aspx?Id="+Id+ "&Status="+status+"&BillType="+BillType +"&Url="+ Url +"",'_blank'); 
        win.focus(); 
      } 
     }); 

2:第二個方法是不使用任何Ajax調用所有,而不是使用按鈕,並在錨標記的href屬性的定位標記給人以查詢字符串值的網頁鏈接。像這樣

<a class='btn' href='BillReceiptReport.aspx?id="+ $("#txtReceiptNo").Text+"' target='_blank';>Click Me</a> 

希望它有幫助。

相關問題