Download a Word editable document from an aspx page - c#

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");

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>

Parent page including iframe form with AsyncFileUpload. redirect to "_top" after uploading and clicking "send"

I have a parent page (testFrame.aspx) including an iframe.
The iframe is an aspx form (form.aspx) with an AsyncFileUpload control (ajaxcontroltoolkit).
The form contains a textbox and a required field validator.
In the bt_Send Codebehind, I check if the user entered a text in the text box and if not I show an error message.
Now, if I test my parent form and I enter a text and click "send" I see my feedback message (the text I entered in the textbox).
If I don't enter a text, and I click send, I get the error message.
The weird thing happens now: if I don't enter a text but I upload a file with the AsyncFileUpload.
I click "send" and instead of getting the error message, I'm redirected to form.aspx outside my parent page (like target="_top"), without error message. I just see the clean form not anymore in my iframe.
I don't want that. I want the form to stay inside my parent page
testForm.aspx looks like this:
<!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>
This is a container page. The following is inside an iframe:
<br />
<iframe src="form.aspx" width="800px" height="400px"></iframe>
</div>
</form>
</body>
</html>
form.aspx looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="form.aspx.cs" Inherits="TestToolkit.form" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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 id="Head1" runat="server">
<title>Test Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br /><br />
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
Upload picture: <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" ClientIDMode="AutoID" />
<div id="uploadOk" style="position: relative; left: 330px; bottom: 40px; width:0px; height:0px; display: none;"><asp:Image ID="imgOk" ImageUrl="_img/check-mark-40.png" runat="server" style="" AlternateText="Erledigt!" /></div>
<br /><br />
<asp:Button ID="btSend" runat="server" onclick="btSend_Click" Text="Send" />
<br /><br />
<asp:Label ID="lblFeedback" runat="server"></asp:Label>
<asp:RequiredFieldValidator runat="server" ID="rfvFirstName" ControlToValidate="txtFirstName" Display="None" EnableClientScript="false" />
</div>
</form>
</body>
</html>
and the code behind form.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestToolkit
{
public partial class form : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btSend_Click(object sender, EventArgs e)
{
//ShowOrRemoveErrors();
if (IsValid)
{
lblFeedback.Text = txtFirstName.Text;
}
else
{
lblFeedback.Text = "Please enter your name";
}
}
}
}
Well I guess I found the answer to my question.
The problem comes if a file was uploaded AND the fields are not valid (or required fields have no text).
The problem comes on postback. So what I did is to implement a client side validation using EnableClientScript="true":
<asp:RequiredFieldValidator runat="server" ID="rfvFirstName" ControlToValidate="txtFirstName" Display="None" EnableClientScript="true" />
With client side validation there is no postback unless all my required fields are filled correctly.
Hope this helps people with my same problem.

Using UrlEncode in HyperLink Button in .NET and in Anchor Tag of HTML

I am just trying to use UrlEncode in a Hyperlink which is in GridView and found it was not working. Then I tried to take the HyperLink as a separate control and tried with that. It is not giving Hyperlink to me, I mean it is not even clickable.
While when I tried with simple Anchor tag, it is working. This is what I am using
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' > wc
</asp:HyperLink>
// While following is working
<a title="asxd" href='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>'>wc
</a>
Still looking for the answer
You don't need to use a Hyperlink control or make an anchor tag runat="server" unless you're doing something to it in your code behind.
wc
You are missing the text property of the hyperlink control.
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' Text="wc" />
You also can set NavigateUrl property in code-behind file, in Page_Load event handler, for instance. It will work.
In the code-behind class:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink2.NavigateUrl = "~/Default.aspx?customer=&CompanyName=" + Server.UrlEncode("abc#");
}
And in markup:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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">
<div>
<asp:HyperLink ID="HyperLink2" runat="server" Target="_new">wc
</asp:HyperLink>
</div>
</form>
</body>
</html>
<%= ... %>
doesn't work inside ASP.NET controls.
Alternatives:
<%# ... > works in some cases in data-bound controls
code-behind

ASP.NET and AJAX library - Extremely slow !

So, i'm new to ASP.NET and AJAX.
I am trying out the Beta library.
I setup a page and was using the Editor.
The loading of this page is well, slow.
There is nothing more than then, you can see it here
http://eski.internet.is/default.aspx but it will take a min to load.
Whats is the reason for the slow load, is it the AJAX library ?
Its about 7 mb, the .dll's. Is it downloading it everytime you load the page ?
No, it doesn't load the whole 7mb worth of .dlls - that's the code that generates the output.
For that site I'm getting this from YSlow:
HTTP Requests - 46
Total Weight - 304.9K
1 HTML/Text 121.9K
4 JavaScript File 161.7K
3 Stylesheet File 6.4K
38 Image 14.7K
Which isn't that much. It did seem to take a LONG time for the host to respond, however. What are the specs on your server and its internet connection?
In your web.config do you have debug="true"? If so, take that out as it can cause pages to take longer as it then generates debug information.
It doesn't seem to be hanging on any one component being served to the client. It seems to be either server load or something in your code. Can you provide the code that you are using to better help diagnose this?
This is the code that website http://eski.internet.is/default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ScriptMode="Release"></asp:ToolkitScriptManager>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropShadowExtender ID="TextBox1_DropShadowExtender" runat="server"
Enabled="True" TargetControlID="TextBox1">
</asp:DropShadowExtender>
<asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server"
Enabled="True" TargetControlID="TextBox1">
</asp:CalendarExtender>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="To Editor" />
<br />
<br />
<cc1:Editor ID="Editor1" runat="server" Width="500" />
<br />
<asp:Button ID="Button2" runat="server" Text="To Textbox"
onclick="Button2_Click" />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server" Height="161px" TextMode="MultiLine"
Width="600px"></asp:TextBox>
</form>
</body>
</html>

Categories

Resources