so i move all code to generate the pdf into a function and on the click button event set a bool with true or false if i want tto generate the pdf but how do i say to render the page normally ?
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If generatepdf = True Then
'pdf generation code called here
Else
'let page render normally
MyBase.Render(writer)
End If
End Sub
hsl89
Member
476 Points
441 Posts
create pdf
Feb 06, 2011 02:22 PM|LINK
i found this useful code:
01.public override void VerifyRenderingInServerForm(Control control) 02. { 03. 04. /* Verifies that the control is rendered */ 05. 06. } 07. 08. 09.protected void imgbtnpdf_Click(object sender, ImageClickEventArgs e) 10. { 11. 12. 13. Response.ContentType = "application/pdf"; 14. 15. Response.AddHeader("content-disposition", 16. 17. "attachment;filename=GridViewExport.pdf"); 18. 19. Response.Cache.SetCacheability(HttpCacheability.NoCache); 20. 21. StringWriter sw = new StringWriter(); 22. 23. HtmlTextWriter hw = new HtmlTextWriter(sw); 24. 25. GridView2.AllowPaging = false; 26. 27. GridView2.DataBind(); 28. 29. GridView2.RenderControl(hw); 30. 31. StringReader sr = new StringReader(sw.ToString()); 32. 33. iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 10f, 0f); 34. 35. HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 36. 37. PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 38. 39. pdfDoc.Open(); 40. 41. htmlparser.Parse(sr); 42. 43. pdfDoc.Close(); 44. 45. Response.Write(pdfDoc); 46. 47. Response.End(); 48. 49.and i'm getting this error RegisterForEventValidation can only be called during Render();
i cant disable validation method at page drectives, so how can i solve this? where is the bug here?.....i use itextsharp 5.0.5
This will export the gridview with the associated images?
mbanavige
All-Star
134980 Points
15429 Posts
ASPInsiders
Moderator
MVP
Re: create pdf
Feb 06, 2011 02:32 PM|LINK
in the click event, set a page level boolean variable to True to indicate that you want to generate a pdf when the page renders.
then move all the code to generate the pdf out of the click event and into an override of the render event
if the page level variable is set to True, then run the pdf generation code - else just call the base render method and let the page render normally.
hsl89
Member
476 Points
441 Posts
Re: create pdf
Feb 06, 2011 03:05 PM|LINK
sorry i dont uderstand...
so i move all code to generate the pdf into a function and on the click button event set a bool with true or false if i want tto generate the pdf but how do i say to render the page normally ?
mbanavige
All-Star
134980 Points
15429 Posts
ASPInsiders
Moderator
MVP
Re: create pdf
Feb 06, 2011 03:08 PM|LINK
something like this:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) If generatepdf = True Then 'pdf generation code called here Else 'let page render normally MyBase.Render(writer) End If End Subhsl89
Member
476 Points
441 Posts
Re: create pdf
Feb 06, 2011 03:29 PM|LINK
i use csgarp so i use this code:
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if (generatepdf == true) {
//pdf generation code called here
} else {
//let page render normally
base.Render(writer);
}
}
however how can i call it (the render void9 when button is pressed i use button click event but i dont find anyway to call the render void
mbanavige
All-Star
134980 Points
15429 Posts
ASPInsiders
Moderator
MVP
Re: create pdf
Feb 06, 2011 03:32 PM|LINK
the framework calls render every time a page is run. it is not something you need to call directly.
hsl89
Member
476 Points
441 Posts
Re: create pdf
Feb 06, 2011 03:47 PM|LINK
you dont understand
if i have the code to generate the pdf out of button click event i need to call the
Protected Overrides Sub Render because its where the code to the pdf is so i change it to public
and the Render(HtmlTestWriter writer is giving me an error its says it is a type and i'm using it like a method
mbanavige
All-Star
134980 Points
15429 Posts
ASPInsiders
Moderator
MVP
Re: create pdf
Feb 06, 2011 03:58 PM|LINK
i do understand ;-)
Render is a standard page lifecycle method that the framework will call every time the page runs. we never call render ourselves for the page.
when your page runs you will get the lifecycle events fired in this order
Init
Load
Click - you set your flag
PreRender
Render - you check your flag
hsl89
Member
476 Points
441 Posts
Re: create pdf
Feb 06, 2011 04:01 PM|LINK
please read my edited post above
mbanavige
All-Star
134980 Points
15429 Posts
ASPInsiders
Moderator
MVP
Re: create pdf
Feb 06, 2011 04:10 PM|LINK
when you override Render, the framework will still call the Render method and will thus invoke your override.
so in the click event you do not need to call render. you only need to set the flag.
protected void imgbtnpdf_Click(object sender, ImageClickEventArgs e) { generatepdf=true; // Render(HtmlTextWriter writer); // no - dont call render yourself }after the click runs and you set your flag, the page lifecycle will continue and the framework will subsequently invoke the Render method.
at that point, your render override is invoked and your code checks the flag and renders as appropriate.