简体   繁体   中英

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

In my Asp.Net(C#) web page, I am doing 95% work from Jquery Ajax . Only Print work is happening from server side code because it needs to redirect another page for print. Here is my print button code

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

When I click Print Button it redirect to printpage with some values in another tab.

But the problem is, when I click Print button Postback happens and everything disturbed in my webpage because as I mentioned above I am doing 95% work using Jquery Ajax.

So I decided to do 100% work from Jquery Ajax and tried to call this print functionality inside Static, WebMethod but I found that RegisterClientScriptBlock is not working inside Static Method. I am trying to do something like this......

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

Please help me guys....

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

You are using this (non-static) inside your static method ie in PrintReport() method

The keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class.

Please try to use the below code:

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

This is not gonna work for you. You are registering a client script block inside a webmethod. client script block runs on page load whereas in ajax call page doesn't reload so its neither giving you any error nor its running your script block. you can adopt two approaches to solve this problem.

1: Do not register any script block inside your web method , just simply return values from your web method eg (Id, status, BillType,Url) and inside the success block of your ajax call open the new page which you were trying to open from inside your 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: Second approach is that do not use any ajax call at all , use an anchor tag instead of button and in the href attribute of of anchor tag give your page link with the query string values. like this

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM