繁体   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