Open Pdf in new tab - c#

On Button Click I generate a PDF file and then open it at client side with:
Response.ContentType = "application/pdf";
Response.TransmitFile(path);
This will show the pdf in the browser, but in the same tab. How can I open it in a new tab?

<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language=vbscript>
sub OpenInNewWindow()
window.open("PDFContainer.aspx")
end sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Way 3</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type=button OnClick="OpenInNewWindow()" value="Open in New Window"/>
<br /><br />
This time, we will open a new Internet Explorer Window and Show the PDF File directly in the IE
</div>
</form>
</body>
</html>

You cannot do this from the server side. It is a client side operation. see Create a new tab or window for a web page from ASP.NET

Related

asp:Button how to capture Response.WriteLine

I am using the OnClick of the asp:Button tag to compute a report which I would like to display as an innerHTML of a div tag. I would like to use the Response.WriteLine method to return this html file. Does ASP.NET webforms provide a way of capturing this response?
Am I forced to use JavaScript instead? I am using C# on the Server side.
The Web Forms way of doing this is to use a control and set its InnerHTML from the code behind. Here's a self contained example:
<%#Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void GenerateReport(object sender, EventArgs e)
{
var reportHTML = "<p>This is a <span style='font-weight: bold;'>report!</span></p>";
ReportDiv.InnerHtml = reportHTML;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<body>
<form runat="server">
<asp:Button runat="server" OnClick="GenerateReport" Text="Generate Report" />
<div runat="server" id="ReportDiv"></div>
</form>
</body>
</html>

having an asp back button in popup window for displaying pdf

Am having a popup window for displaying pdf.. but i need to put an asp button in pop up window have tried the following code but could not find a solution.. can any one resolve this?
enter code here
<asp Page attributes>
...............
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<br />
<form id="form1" runat="server">
<div> <asp:Button ID="dd" runat="server" OnClick="Backonclick" Text="back"/>
</div>
<div></div>
</form>
</body>
</html>
on page load event
dd.Visible = true;
string path = Request.QueryString["val"].ToString();
string extention = Path.GetExtension(path);
int len = extention.Length - 1;
string extwithoutdot = extention.Substring(1, len);
if (extwithoutdot.Equals("JPG") || extwithoutdot.Equals("jpg") ||
extwithoutdot.Equals("jpeg") || extwithoutdot.Equals("JPEG"))
{
extwithoutdot = "jpeg";
...........
The issue here is that you're mixing two separate types of documents; a PDF is one type of download, whilst an HTML is another. A single download can't unfortunately be both, and you can't switch document types part-way through and expect the browser to handle it.
The simplest solution for what you're after is to combine the two on the client side. Load your pop-up as an HTML page (generated by ASP.NET if you wish) and include within that an IFRAME linking to your PDF generation script; something of the form:
<html>
<body>
<a class='button' href="javascript:window.close()">Close</a>
<iframe src="generatePdf.aspx" height="300" width="300" />
</body>
</html>
i have got solution
<form id="form1" runat="server">
<iframe src="Copy (2) of DisplayPdf.aspx" width="1000" height="400">
</iframe>
<iframe src="Copy of DisplayPdf.aspx" width="100" height="200">
</iframe>
</form>

Load .ascx from string (SQL)

I like to to ask that is there a way to load an ascx control from string? To be more precise: I like to make/store a new .ascx in sql then load it to the website.
Yes, you should be able to use the ParseControl function to do what you want:
http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx
Example from MSDN:
<%# Page language="c#" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ASP.NET Example</title>
<script runat="server">
// System.Web.UI.TemplateControl.ParserControl;
// The following example demonstrates the method 'ParseControl' of class TemplateControl.
// Since TemplateControl is abstract, this sample has been written using 'Page' class which derives from
// 'TemplateControl' class.
// A button object is created by passing a string to contstruct a button using ASP syntax, to the
// 'ParseControl' method. This button is added as one of the child controls of the page and displayed.
void Page_Load(object sender, System.EventArgs e)
{
Control c = ParseControl("<asp:button text='Click here!' runat='server' />");
myPlaceholder.Controls.Add(c);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id ="myPlaceholder" runat="server" />
</form>
</body>
</html>

Download a Word editable document from an aspx page

I currently have an aspx page returning a html form (with images and css). I would like get a download Word editable version of this form without rewrite all the code. Is it possible ?
An example : I Have a page 'WebForm2.aspx' with a simple content like this.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="WebApplication15.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Title</h1>
<asp:CheckBox runat="server" ID="myTest" Text="Doc 1" /><br />
<asp:CheckBox runat="server" ID="myTest2" Text="Doc 2" /><br />
<asp:CheckBox runat="server" ID="myTest3" Text="Doc 3" /><br />
<asp:CheckBox runat="server" ID="myTest4" Text="Doc 4" /><br />
</div>
</form>
</body>
</html>
I would like that from an other page for example a button that lets you download the contents of 'WebForm2.aspx' in an editable Word document.
I Add this in Page_PreRender method
Response.AddHeader("Content-Type", "application/msword");
It's partially work. If I save the document css and images aren't loaded but if I only open the downloadable content css and images are loaded.
MS Word can edit html document so by adding Content-Type=application/msword record in header will make browser open your page in browser unfortunately without css and images.
Add following code in prerender event on your page
Response.AddHeader("Content-Type", "application/msword");

ASP.Net webform generated Excel File to local PC and not server

Following on from a question about generating Excel files, I need to be able to create the file locally while the webform app is located on the remote web server. This is not anything I've dealt with before, so I am finding it difficult exactly what to ask. I am using WebForms on VS2010 with c#. eanderson pointed me in the direction of
Simplexcel by Michael Stum which seems to do the trick but the file is generated on the server (or should I says 'tries to' as it is not permitted!!!).
You should be able to do something similar to this to generate and download the Excel Sheet.
protected void generateExcelSheet_click(object sender, EventArgs e)
{
// Create Excel Sheet
var sheet = new Worksheet("Hello, world!");
sheet.Cells[0, 0] = "Hello,";
sheet.Cells["B1"] = "World!";
var workbook = new Workbook();
workbook.Add(sheet);
// Save
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=MyExcelSheet.xls");
workbook.Save(Response.OutputStream, CompressionLevel.Maximum);
Response.End();
}
In the designer.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FileDownload.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnExcel" runat="server" Text="Download Excel Sheet" onclick="generateExcelSheet_click" />
</form>
</body>
</html>
I based this code on this tutorial.

Categories

Resources