locked
Using the Response object in a static webmethod? RRS feed

  • Question

  • User-1945555371 posted

    Hi all,

    I'm calling a method in my code behind from javascript. I'm trying to load a CSV. Here's the code I have:

    [WebMethod]
            public static void MyMethod(string name)
            {
                Context.Response.Clear();
                Context.Response.ContentType = "Text/vnd.ms-excel";
                Context.Response.AddHeader("Content-Disposition", "attachment;filename=test.csv");
                Context.Response.Write("test1,test2,test3");
                Context.Response.End();
            }

    The problem is an object reference is required for the non-static field Context.Response:

    Error    5    An object reference is required for the non-static field, method, or property 'System.Web.UI.Control.Context.get'

    Is there any way around this?

    Thanks in advance,
    Wilky.

    Monday, October 10, 2011 11:40 AM

Answers

  • User-666215580 posted

    Hi In order to call the method using PageMethods. First of all the method should be declared as static.

    So you declared MyMethod as static and it is fine.

    But you are trying to make use of Context

    Context( System.Web.UI.Control.Context.get) in this case is nothing but a property that returns HttPResponse object. So it is non static so you cannot use that in static method without creating object. So you can do something like this

    HttpContext context = HttpContext.Current;
                context .Response.ContentType = "Text/vnd.ms-excel";
                context .Response.AddHeader("Content-Disposition", "attachment;filename=test.csv");
                context .Response.Write("test1,test2,test3");
                context .Response.End();

    Please visit this url https://pavanarya.wordpress.com/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 5, 2012 7:29 AM

All replies

  • User465171450 posted

    The problem though is where is any of this going? You should be returning something to the webmethod such as a binary object that contains the file content so that it is properly formatted and structured data being returned. Here's an example of passing files as binary objects.

    http://www.codeproject.com/KB/webservices/Transfer_any_files_on_Web.aspx

    This method would be fine for a web page because the web server is expecting data with a contenttype, a web service of type void is not expecting anything to be returned. The other end of the webservice call wouldn't know what to do with whatever was returned as it's not supposed to return anything.

    Monday, October 10, 2011 4:28 PM
  • User-1945555371 posted

    Hi Mark,

    I don't want to return anything to the webmethod. Basically I want to click a button which will show a loading image and fire a webmethod, the webmethod will create a CSV then return to the javascript to stop the image.

    <head runat="server">
        <title></title>
        
        <script language="javascript" type="text/javascript">
    
            function ShowLoading() {
            
                var imgLoading = document.getElementById("imgLoading");
                imgLoading.style.display = "block";
    
                PageMethods.MyMethod(HideLoading);
            }
    
            function HideLoading() {
                var imgLoading = document.getElementById("imgLoading");
                imgLoading.style.display = "none";
            }
        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
           
           <asp:ScriptManager ID="ScriptManager1" 
            EnablePageMethods="true" 
            EnablePartialRendering="true" runat="server" />
        
            <asp:LinkButton ID="lbtnKPIReport" runat="server" 
                OnClientClick="ShowLoading(); return false;"
                ToolTip="run the KPI report" >Run KPI report</asp:LinkButton>
            
            <asp:Image runat="server" ID="imgLoading" ImageUrl="~/images/loading.gif" 
                AlternateText="KPI report is loading..." />
        </div>
        </form>
    </body>
    </html>
    Tuesday, October 11, 2011 4:38 AM
  • User-666215580 posted

    Hi In order to call the method using PageMethods. First of all the method should be declared as static.

    So you declared MyMethod as static and it is fine.

    But you are trying to make use of Context

    Context( System.Web.UI.Control.Context.get) in this case is nothing but a property that returns HttPResponse object. So it is non static so you cannot use that in static method without creating object. So you can do something like this

    HttpContext context = HttpContext.Current;
                context .Response.ContentType = "Text/vnd.ms-excel";
                context .Response.AddHeader("Content-Disposition", "attachment;filename=test.csv");
                context .Response.Write("test1,test2,test3");
                context .Response.End();

    Please visit this url https://pavanarya.wordpress.com/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 5, 2012 7:29 AM
  • User-1945555371 posted

    Hi paryasomayajulu.

    I no longer work for the company I was doing this for, but I make you right in your answer.

    Thanks!

    Wednesday, May 9, 2012 5:20 AM