1

i have some html content and i stored it in string variable and i want to print it directly.Is there any way in c# ?i have a javascript code which is not working

string emailbody="HTML i need to send";
Page.RegisterStartupScript("StatusMessage", "<SCRIPT LANGUAGE=\"JavaScript\">function     printsheet(" + emailbody + "){var win = window.open('mywindow', 'left=0', 'top=0')var html =   Zstring; win.document.open()win.document.write(html);win.print();}</Script>");
2
  • 2
    try this: printsheet('" + emailbody + "') Commented Apr 26, 2013 at 12:19
  • what is the error? inspect printsheet method, use Alert() to see what does the method gets in paramethers Commented Apr 26, 2013 at 12:24

1 Answer 1

4

You have many ways to do that.

One way, make the string public

public string emailbody="HTML i need to send";

and on aspx page you render it as:

<%=emailbody%>

One other way is to use a Literal control and render it there. When you have UpdatePanel this is the only way.

Eg, you place the Literal on page, on the point you wish to render your text as:

<asp:Literal runat="server" id="txtRenderOnMe" />

and on code behind you type:

txtRenderOnMe.Text = "HTML i need to send";

Now, in your case the issue is that you render a string on the javascript code without the quotas as the other jesse point out on their comments.

string emailbody="HTML i need to send";
Page.RegisterStartupScript("StatusMessage", "<script language=\"JavaScript\">function     printsheet('" + emailbody + "'){var win = window.open('mywindow', 'left=0', 'top=0')var html =   Zstring; win.document.open()win.document.write(html);win.print();}</script>");
Sign up to request clarification or add additional context in comments.

4 Comments

Nice answer, but why "<%=emailbody%>" wont work inside an update panel ?
@HassanMokdad Is not working on UpdatePanel update, because UpdatePanel is not render again the page on code behind, and this part of this code can not be run. What updatepanel do is to run the code behind, know how the part of the page that needs update is, and send this part with ajax.
@HassanMokdad well maybe is not so good explain. Let me say it again. The update panel needs to know a part of a page how exactly is. If you use the <%=%> this part of the page is change when the page is send on the client. But the UpdatePanel is not get the full page but only a part. This part must be fully defined and not change, but only on code behind. On code behind you can change it.
@HassanMokdad here is one more: stackoverflow.com/questions/8202762/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.