2

everyone, thank for your time.

Well this my problem (well it's not a probleam at all), it is possible to have a class that inherits from ui.page and then instance an object of that class and do a redirect to it ?

Something like this:

Public sub myMethod() 
    Dim myPage as new myClassThatInheritsFromUIPage()
    Response.redirect(myPage) 'Here is one of my "no-idea" line
end sub

I do this in my webForm (and that what I want to do in a class that inherits from ui.page):

Response.BufferOutput = True  
Response.ClearContent()
Response.ClearHeaders()
ostream=crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
Response.AppendHeader("Cache-Control", "force-download")
Response.AppendHeader("Content-disposition","attachment;filename=ReportAsPDF.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(ostream.ToArray())
Response.Flush()  
HttpContext.Current.ApplicationInstance.CompleteRequest()

What I want to do is perfectly possible with a normal WebForm, but my webForm doesn't render nothing at all (at least as (x)html so, that's because I would like to know if what I'm asking is possible to achieve.

Thank you everyone.

Well at the end I just add "HttpContext.Current." to all the lines that include a "response" attribute, so now I have just a class that NOT inherits from "UI.Page" and just call the method that clear the buffer (a custom method), add the headers (to force the download,set the mime type and filename) and flush the response by itself and get the effect/use I want it.

In order to access to the Session vars just add "HttpContext.Current." and it works, I don't know how secure or if is a good way,but appears to work well.

So the code now looks something like this:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource

Namespace foo
  Public Class requestReport


    'just to instance the object'
    Public Sub New()


    End Sub


    Public Sub downloadReport()
     'do all the stuff to connect and load the report'
         HttpContext.Current.Response.BufferOutput = True  
         HttpContext.Current.Response.ClearContent()
         HttpContext.Current.Response.ClearHeaders()
  ostream=crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
        HttpContext.Current.Response.AppendHeader("Cache-Control", "force-download")
        HttpContext.Current.Response.AppendHeader("Content-disposition","attachment;filename=ReportAsPDF.pdf")
        HttpContext.Current.Response.ContentType = "application/pdf"
        HttpContext.Current.Response.BinaryWrite(ostream.ToArray())
        HttpContext.Current.Response.Flush()  
        HttpContext.Current.ApplicationInstance.CompleteRequest()
     End Sub
   End Class 
 End Namespace

And from a command for example do this:

dim myReport as new foo.requestReport()
myReport.downloadReport() 

Of course now you can add more attributes or method if you need it.

So now I don't even don't use Response.redirect() or inherits from "UI.Page", just a class that "change" the "current response" and "flush" the content created on fly with the crystal report, I think I was kind of totally lost but your answers really help me, especially what Tejs says, what is almost the same or the same what I just did. Thank you.

UPDATE: By the way, I just realize that the ReportDocument class has this method: ExportToHttpResponse that let us flush the Crystal Report to the browser as PDF/XSL etc forcing (or not) the download of the file.

Allende
  • 1,480
  • 2
  • 22
  • 39

4 Answers4

2

No, as there is no current overload that accepts a UI.Page instance. However, you could call the Render method on your new page and write directly to the response stream. AKA

  1. Clear the Response Buffer.
  2. Render your page instance to the response buffer.
  3. Invoke Response.End() to flush the request and send it to the client.

If your new page doesnt actually do anything, you can additional just send no content back with the response.

Tejs
  • 40,736
  • 10
  • 68
  • 86
  • I don't get it, I can't see the "Render" method on my object. Could you extend your answer please, and if I do that the "actual" response would get lost ?. My object from the class that inherits actually is a file (xls,pdf) for download (that's because I don't want to use a webForm at all) I create the file "on fly" using Crystal report export method inside, it works with a webForm (change the headers, flush the response), but I'm trying to use a class that just inherits from ui.page, it's kind of good idea or I'm totally wrong? – Allende Aug 22 '11 at 19:36
  • I got it I just use the same as on the webForm but for all the lines with or that involve the "response" attribute add "HttpContext.Current." – Allende Aug 22 '11 at 20:31
2

You can use Server.Transfer and pass in the instance of the page object that you want to tranfer to.

Here is the documentation: HttpServerUtility.Transfer

Daniel
  • 1,783
  • 1
  • 14
  • 15
  • Your answer looks userful but I think it's "too much power" for my problem, actually just a few days ago I was wondering how to do exactly that (a "custom" httpRequest). Thank you. – Allende Aug 22 '11 at 21:01
1
 HttpRequest Request = HttpContext.Current.Request;
 HttpResponse Response = HttpContext.Current.Response; 

And after that you can redirect any page.

Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
BabyAngel
  • 11
  • 1
  • 1
    HttpRequest Request = HttpContext.Current.Request; HttpResponse Response = HttpContext.Current.Response; – BabyAngel Apr 19 '12 at 08:21
1

Try just doing this instead:

HttpContext.Current.Response.Redirect("...");
James Johnson
  • 45,496
  • 8
  • 73
  • 110