2015-08-19 36 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> 

希望它有帮助。

相关问题