Response is not available in this context

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BeemerBiker
    New Member
    • Jul 2008
    • 87

    Response is not available in this context

    I get that error message when trying to do a "Response.write ()". I undoubtedly set up my classes wrong. My workaround is to put the offending code in each codebehind page. I would like to put the code in my "constant class" so it can be called as a function by any page. Details follow:

    I added a gridview print routine to my dotnet 2 app. It works fine after
    googling why it didnt work for an hour or more. Since I got several grids on various pages I thought I would add it to my "constant class" which all my codebehinds have and I put all my common code in it.

    Code:
    namespace Training
    {
    ..
     public class ConstClass
     {
      ...
        public void print(GridView gv, strFilename)
      {
         Response.Clear();
         ..etc..
       }
      ...
     }
    }
    the above would not compile until I added System.Web.UI.Page as in
    
    public class ConstClass : System.Web.UI.Page
    ok, it finally recognized "Response" and compiled. Unfortunately, it wont
    run and gives me the error message "Response is not available in this
    context" at the line "Response.Clear ()"

    There is something wrong with how I defined my class I suspect. All I
    wanted to do was to call the "print" function from any of my pages and not have to put the print function in each C# page. I am doing something wrong, yet all my other functions get called and work ok (they do not have "response") .


    I call the print function as outlined below
    Code:
    protected void Page_Load(object sender, EventArgs e)
            {
                if (null == Session["cUsefulFunctions"])
                    Session["cUsefulFunctions"] = new ConstClass(Session);
                cUsefulFunctions = (ConstClass)Session["cUsefulFunctions"];
      ..etc..
    
     protected void btnPrint_Click(object sender, EventArgs e)
            {
                cUsefulFunctions.print(gv4needed, "needed.xls");
            }
    Regards
    Joseph
  • alamodgal
    New Member
    • Dec 2008
    • 38

    #2
    Hiiiiiii
    Instead of writing Response.Clear( );
    Write
    HttpContext.Cur rent.Response.C lear();

    Comment

    • hajoabdul
      New Member
      • Mar 2009
      • 7

      #3
      actually i dont understand the reason for you using response.clear( )

      Comment

      • BeemerBiker
        New Member
        • Jul 2008
        • 87

        #4
        Originally posted by alamodgal
        Hiiiiiii
        Instead of writing Response.Clear( );
        Write
        HttpContext.Cur rent.Response.C lear();
        thanks alamodgal - that worked!

        ..but..
        ..but..
        ..but..

        I had to leave this code in the C# file
        Code:
        public override void VerifyRenderingInServerForm(Control control)
                {
                }
        Putting that above code in the ConstClass module allowed the print routine to run without error, but nothing got printed.

        Comment

        • BeemerBiker
          New Member
          • Jul 2008
          • 87

          #5
          Originally posted by hajoabdul
          actually i dont understand the reason for you using response.clear( )
          Mine is not to reason why, mine is just to copy and compile

          Comment

          • hajoabdul
            New Member
            • Mar 2009
            • 7

            #6
            y i did ask that question was that i did something similar to wat u did, but instead of responsee.clear (since am not familar with it) i wrote a function that does it for me. so i dont know abt the existence of the code "Response.Clear ()" am nt trying to be rude

            Comment

            • alamodgal
              New Member
              • Dec 2008
              • 38

              #7
              Use HttpContext.Cur rent.Response.W rite instead of clear.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                I hope that you're aware that if you use the Response.Clear, it is going to erase all of your HTML in the <body> of your HTML page......anyth ing added to the page before that point is going to be erased.

                Anyways, the reason the short hand method call "Response.Write ()" is not working in the class is because it's just a normal class....the HttpContext is not automatically available to the class. Since the method is part of the HttpContext.Cur rent.Response, you must call the method as "HttpContext.Cu rrent.Response. Clear();".

                I honestly think you are going about this in the wrong way.

                You should consider creating a Web User Control to display your grid on the pages that you require it to be accessed on.

                So, say you want to show it on Page 1.....then again on page 4 but not on page 2 or 3.....You would simply drag your Web User Control that displays the grid onto those pages. The Web User Control would do all the work for you and you wouldn't have to call anything....not only that but you could place the control in specific places on the page this way as well.

                -Frinny

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Wait a second, I think you're going about this completely wrong (after look at the CodeProject article you linked us to)

                  Put that code into an ASPX page of it's own.

                  It will overwrite the content that is sent to the browser and return an Excel sheet instead of regular HTML....once you've done this you can call the ASPX page from buttons in the page and it will let you download the exported Excel.


                  What are you trying to do?????

                  Comment

                  • BeemerBiker
                    New Member
                    • Jul 2008
                    • 87

                    #10
                    Originally posted by Frinavale
                    Wait a second, I think you're going about this completely wrong (after look at the CodeProject article you linked us to)

                    Put that code into an ASPX page of it's own.

                    It will overwrite the content that is sent to the browser and return an Excel sheet instead of regular HTML....once you've done this you can call the ASPX page from buttons in the page and it will let you download the exported Excel.


                    What are you trying to do?????
                    I am putting together a "print friendly" page that does not have scroll bars, drop down lists, etc where one can click on "print" and get something that looks good when you print it. The following is not print friendly


                    I thought for a first go at it I would just convert the grid into word or excel and let the user print it up from the excel or word app.

                    However, after talking this over with the linux people (they have no interest in having excel or word on their system), I will have to roll my own print routine and not rely on that grid to excel mechanism. Since we also want "reports" then I will probably create my own grid on a huge form where it all fits and set printer friendly css and use that form as a general purpose printing mechanism. I will probably use the "grid to text" that is in that codeproject example as a starter. However, admin (they only use windows) does like the excel and will want some reports in excel since they know how to get excel into powerpoint.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      There is a much easier way to do this.
                      You can create a CSS style sheet that is applied to your page only when the user prints it.

                      This A List Apart article describes what I'm talking about in great detail.

                      Consider this, because it may save you a lot of development time!

                      -Frinny

                      Comment

                      Working...