簡體   English   中英

是否可以在ASP.NET C#的靜態方法中使用RegisterClientScriptBlock?

[英]Is it possible to use RegisterClientScriptBlock in Static Method in asp.net C#?

在我的Asp.Net(C#)網頁上,我正在通過Jquery Ajax執行95%的工作。 服務器端代碼僅發生打印工作,因為它需要重定向另一個頁面進行打印。 這是我的打印按鈕代碼

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);
    }

當我單擊“打印按鈕”時,它將重定向到另一個標簽中帶有某些值的打印頁面。

但是問題是,當我單擊“打印”按鈕回發時,網頁中的所有內容都被打亂了,因為如上所述,我使用Jquery Ajax進行了95%的工作。

因此,我決定從Jquery Ajax進行100%的工作,並嘗試在Static WebMethod內調用此打印功能,但是我發現RegisterClientScriptBlock在Static Method內不起作用。 我正在嘗試做這樣的事情……

[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);
    }

請幫助我。

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

您正在靜態方法(即PrintReport()方法)中使用this (非靜態)方法

關鍵字“ 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);
            }
        }
    }

這不會為您工作。 您正在Web方法中注冊客戶端腳本塊。 客戶端腳本塊在頁面加載時運行,而在ajax調用頁面中不會重新加載,因此它既不會給您任何錯誤,也不會運行腳本塊。 您可以采用兩種方法來解決此問題。

1:不要在您的Web方法中注冊任何腳本塊,只需從Web方法中返回值(例如(Id,status,BillType,Url)),然后在ajax調用的成功塊中打開您嘗試打開的新頁面從您的網絡方法內部。

$.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調用,使用錨標記而不是button,並且在錨標記的href屬性中為您的頁面鏈接提供查詢字符串值。 像這樣

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

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM