Creating a report from label values in asp.net - asp.net

I have a website that I wrote in ASP.net and I have a page called Statistics.aspx
that I displayed few useful numbers that admin can view in labels .
I want to make a button " Download Report " that can put these numbers into a word/pdf document .
Can anyone help ?

You need to give a bit more detail.Well I believe this is useful
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
StringBuilder sb = new StringBuilder();
sb.AppendLine(Label1.text); // and so on
using (StreamWriter outfile = new StreamWriter(mydocpath + #"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}

Related

how to call page which creates pdf

I have a page called OrderExport.aspx which creates a pdf file in a folder on the server.
In my page OrderSend.aspx I have a function which sends an e-mail with the pdf file attached, like this:
Dim atc As Net.Mail.Attachment = New Attachment(stm, fileNameOrder)
mail.Attachments.Add(atc)
How can I call OrderExport.aspx from this function before sending the e-mail without showing it for the user?
I need to make sure that the pdf file exists when sending the e-mail.
You probably want to create a class rather than an aspx page to handle the PDF creation. One way to do this is to use the PDFSharp library which can handle creating the PDF document and then saving it to a stream which can be attach to an email message. You could also save the stream to a file on the server at the same time.
' Create a new PDF document
Dim document As PdfDocument = New PdfDocument
document.Info.Title = "TITLE"
Dim securitySettings As Security.PdfSecuritySettings = document.SecuritySettings
' Restrict some rights.
securitySettings.PermitAccessibilityExtractContent = False
securitySettings.PermitAnnotations = False
securitySettings.PermitAssembleDocument = False
securitySettings.PermitExtractContent = False
securitySettings.PermitFullQualityPrint = True
securitySettings.PermitModifyDocument = False
securitySettings.PermitPrint = True
' Create an empty page
Dim page As PdfPage = document.AddPage
' Get an XGraphics object for drawing
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim img As XImage = XImage.FromGdiPlusImage(imgPermit)
gfx.DrawImage(img, 20, 20)
Dim stream As New MemoryStream()
document.Save(stream, False)
Dim xStream As System.IO.Stream = stream
SendEmail(sEmail, xStream)
in your mail message it would be constructed similar to this
Dim xStream As New Attachment(pdf, "FILE" + Date.Now.ToString("MM.dd.yyyy") + ".pdf")
mm.Attachments.Add(xStream)
You need to separate the GUI from the actual logic.
You presumably have code in OrderExport.aspx that creates the PDF and saves it to disk. You should put that code into a separate class, so that you can call it from OrderExport.aspx, and from the page with the "Send e-mail" button. Similarly, extract the code for sending the email from OrderSend.aspx and call it from there, and from the "Send e-mail" button.
Separation of Concerns is the best solution, but if you need a quick solution -not a good one- make the class and the function public and call it like any other class

How to create a dynamic table from java script?

How to create a dynamic table from java script?I am fetching a record from database and storing into variable and adding that value to table cell.
Here I put some text not actual value.
In cs file's button click event I doing like this
StringBuilder sb = new StringBuilder();
sb.Append(#"<script language='javascript'>");
sb.Append("<table><tbody><tr><td>Information</td></tr></tbody></table>");
sb.Append(#"</script>");
if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup"))
ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString());
But I am unable to see Information.
you must add document.Write in your script
so replace with
sb.Append("document.write('<table><tbody><tr><td>Information</td></tr></tbody></table>');");

Server.MapPath in window open in asp.net

i want to open a new pop up window when user click the button.But nw i facing a problem, how can i open a new pop up window based on server.mapPath?
Here is my coding
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("window.open(" + Server.MapPath("~/reportPreview.aspx") + ", '', '');");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(),"test", sb.ToString());
But i cant open a new window . Please help :(
window.open expects a URL like "../reportPreview.aspx", but Server.MapPath returns a physical path like "C:\YourApp\reportPreview.aspx". You should call ResolveClientUrl instead. Also, you need to add quotes around the URL:
sb.Append("window.open('" + ResolveClientUrl("~/reportPreview.aspx") + "', '', '');");

send rendered GridView in an email

I have a GridView whose HTML I need to put into an email.
How can this be done?
use an HTMLtext writer to render the gridview control.You will get an HTML output as string.use this as your message body.
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}
Use a stringbuilder to create a HTML table to resample your gridview like so:
var output = new StringBuilder("<table cellpadding='5' style='border: solid 1px black;'>");
foreach (GridViewRow row in MyGridView.Rows)
{
output.Append("<tr>");
output.Append("<td>" + Title + "</td>");
output.Append("<td>" + Price + "</td>");
output.Append("<td>" + Quantity + "</td>");
output.Append("</tr>");
}
output.Append("</table>");
Ideally you should not be concerned about the GridView control. Take the data that is being displayed in the control, format it in what ever manner you want and pass it as your message body.
I would treat the logic to display Grid in my application and generating grid for message body as two separate functionality. What will be common in between the two, is the source of data. For formatting of data(generating the Grid), you can use the HTMLTextWriter class(as mentioned by others) and format the data in what ever way you want.
Two thing you can do for this
1) take screen shot of the current page by using any utility and send in mail
2) you can export grid as excel and than sent it in mail.
Export grid to Excel
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx

Render ASP.Net PlaceHolder .ToString(), not to page

I've searched around and have not been able to find an good solution. I have a custom extension to a PlaceHolder control that will contain expressions that I would like to take the string output of without having to call control.Render(), since that call automatically writes the contents out to the page.
Does anybody know how to get the would be rendered content into a string and prevent the page from containing it?
The often-regurgitated, slightly dated code for this goes something like:
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}

Resources