How to do Silent Printing? [duplicate] - c#

This question already has answers here:
"Silent" Printing in a Web Application
(11 answers)
Closed 3 years ago.
So basicly, I made a simple asp.net website which prints out the page when you click on the button. When you are printing out it asks me which printer and which options will be used. My aim is to directly print the page out without asking me printers and options.Thank you for your help.
Im running this program on visual studio 2015.
c# asp.net
function printpage() {
var getpanel = document.getElementById("<%= Panel1.ClientID%>");
var MainWindow = window.open('', '', 'height=500,width=800');
MainWindow.document.write('<html><head><title>Print Page</title>');
MainWindow.document.write('</head><body>');
MainWindow.document.write(getpanel.innerHTML);
MainWindow.document.write('</body></html>');
MainWindow.document.close();
setTimeout(function ()
{
MainWindow.print();
}, 500);
return false;
}
HTML (aspx)
<asp:Button ID="Button1" runat="server" OnClientClick="return printpage();" Text="Print Page" />
I do not have any errors program is running clearly.

This is not possible for the general case. It is blocked as a security measure, to keep your printer from collecting the same kind of junk that used to pollute fax machines.
The only work-arounds are via browser plugins (flash, silverlight, unity, java, etc), or if your web server happens to be on the same local network as the end-user's browser.

Related

WPF Webbrowser - Access to Document Body on IE11

I'm developing a Desktop application which stores the GUI layer on html files as embedded resources. And I have a WPF Web Browser that manages the user interface lifecycle. I'm able to do all the job with no problems. But I've recently found out that when enabling the FEATURE_BROWSER_EMULATION to IE 11 (11000), the document body becomes inaccessible. Then, when using IE 10 (10000) everything works nicely.
The example below shows how to get the document body OffsetHeight:
dynamic document = (this.wbContent.Document as dynamic);
if ((document == null) || (document.body == null)) return 0;
return document.body.OffsetHeight;
The HTML is:
<html>
...
<body style="width: 170px; height: 240px">
...
</body>
</html>
When using IE 11 it throws an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'.
When using IE 10 it returns 240.
Have you got any idea about how to access body on IE 11?
As I did not get any answer about solving the problem, I had to trick in order to work around the bug. What is did is to put a simple JavaScript on the html file, so I call the method window.resizeTo(a, b). After this event is called, the application get back working, but I actually don't know why.

Deciding whether javascript is disabled/enabled and execute a server side code [duplicate]

This question already has answers here:
redirect to another page if javascript is disabled [duplicate]
(5 answers)
Closed 2 years ago.
I've an aspx page which loads several images after detecting client's screen size. Here is how I do this:
In jquery code, I first detect user screen size, then depending on the size, I do some calculations to decide the number of images to be fetched from database. Then with the use of jquery ajax, I call up a web service which returns json response. Code picks up json and creates page.
Now, if user has disabled javascript, this approach doesn't work. Now I have to create page using c# code on code behind. The problem is, I can't detect if javascript is disabled from code behind (I think this is not possible). I do not want to display a button at the top says something "Javascript is disable in your browser. Please click here to display this page". I can put this button in noscript tags. But I do not want user have to click a button before seeing images on page.
What I want, if javascript is disabled, system should detect it and immediately run a function from code behind. (IN this case page size would be fixed and won't vary according to user screen) I can not use a hidden field or cookie to detect it since they both will need a postback before detecting javascript is diabled. (We know that js code can't run before any page life-cycle event).
Well I don't have much hope that my problem could be resolved, but still I want to give it a try. May be someone already have a solution. It would be extremely helpful if you could give your views or suggestion to change/update logic.
Any help would be greatly appreciated.
Thanks and Regards
Praveen
1.) Use JavaScript to set a cookie, and then test for that cookie using server-side scripting upon subsequent page views; deliver content appropriately.
How to detect if JavaScript is disabled?
2.) You can use <noscript> tag.
The <noscript> tag: can be used to provide an alternate content for users that have disabled scripts in their browser or have a browser that doesn’t support client-side scripting.
<noscript>
....
</noscript>
3.) Detect if JavaScript is enabled in ASPX
protected void Page_Load(object sender, EventArgs e)
{
if (Session["JSChecked"] == null)
//JSChecked -indicates if it tried to run the javascript version
{
// prevent infinite loop
Session["JSChecked"] = "Checked";
string path = Request.Url + "?JScript=1";
Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect",
"window.location.href='" + path + "';", true);
}
if (Request.QueryString["JScript"] == null)
Response.Write("JavaScript is not enabled.");
else
Response.Write("JavaScript is enabled.");
}
Make default version without javascript and redirect to javascript version if javascript enabled. How to redirect if javaScript is disabled?

Read JavaScript display value from HTML file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
View Generated Source (After AJAX/JavaScript) in C#
Note : I m' not using ASP.NET and i use a c# Console project.
With a simple html document :
<script type="text/javascript">
eval(function(p,r,o,x,y,s){y=function(c){return(c<r?'':y(parseInt(c/r)))+((c=c%r)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(o--){s[y(o)]=x[o]||y(o)}x=[function(y){return s[y]}];y=function(){return'\\w+'};o=1};while(o--){if(x[o]){p=p.replace(new RegExp('\\b'+y(o)+'\\b','g'),x[o])}}return p}('j=D^C;b=3;h=1;r=7;t=B^E;p=9;g=F^A;f=0;l=5;a=G^I;q=y^u;o=w^z;s=6;d=4;k=2;n=8;m=x^v;i=H^L;e=W^U;c=X^V;J=f^j;S=h^i;M=k^g;T=b^a;K=d^e;N=l^m;O=s^t;R=r^q;Q=n^o;P=p^c;',60,60,'^^^^^^^^^^Three4Two^Seven^One3Nine^Nine^Four4Three^Zero^OneTwoSix^Three^FourSixFive^Five8Four^One^Two^One2Eight^Five^ZeroThreeZero^Six^Nine0Seven^Four^Eight^SevenNineOne^8080^8888^10448^7095^12181^3129^88^11687^8090^3394^1337^3885^9893^12222^9090^ZeroOneNineThree^SixOneTwoOne^8000^ThreeEightThreeTwo^OneOneSixSeven^FourThreeEightZero^Eight2FourEight^Seven0ZeroNine^OneEightFiveSix^EightTwoSevenFive^Four0OneFour^808^1080^11238^10637'.split('\u005e'),0,{}))
document.write((Seven0ZeroNine^ZeroThreeZero));
</script>
Which Display simply "8"
I would like to get this "display" value. I need certainly Webrowser which decodes JavaScript.
WebBrowser web = new WebBrowser();
web.Navigate(#"c:\1.html");
Open a browser console (google it) and try:
console.log("string goes here")

Pop Up Window Data Variable Passing

I am creating a web application for my company that needs to deal with form processing and database manipulation. The application is implemented using .NET Framework 3.5 using C# and Visual Studio 2008 as the IDE and Microsoft SQL Server 2005 as the database.
Here is my problem:
I have a lot of forms
But my boss only want me to create a single page for the database
processing (easier to extend in the future)
I figured out the only way to solve this problem is by having only 1 .aspx file (that contains everything about the database) and having it invoked as a pop up window everytime a forms need to deal with the database.
Here is another problem of mine:
Due to stateless nature of HTTP, I am unable to process and pass
variable between 2 different windows.
I managed to create certain Javascript functions and have the
variable transferred on display, however it can only pass a variable
that is the primary key in the table. To process other columns in
the table is possible but as the consequence I have to write a very
long inline script in my .aspx page and after it is compiled people
can easily view how to access my company database easily. Hence, I
don't favor this (beside to deal with 1 form, I need to create a
long code already, imagine if i have more than 1000 forms!)
So there are two ways you guys can help me:
Suggest another way other than popping up a new window for my
problem, maybe even advise on how it's implemented.
If you think popping up is the solution, you mind to share some
snippets that can help me figure out the variable passing between
two different windows. I can use some advise especially from some
Javascript expert on this :).
Note: Solution must be workable in ASP.NET Framework 3.5 and tested using IE browser version: 8.
P.S: This is a short explanation about my application flow
Let's say I entered data about a product (it has few properties id, name, price, etc) into the database
Later on somehow I want to edit one or few properties of that product, so I have to launch a form which called "editor.aspx"
Instead of entering the product id (which is the primary key) into the form (and edit the data based on the entered product id) and risking to miscalculately edit the correct data, I provide a small button in the form (let's name it btSearch), that will launch a new popup window which contains the gridview of the database of all product (with selection enabled)
Now I just need to browse through the gridview, select a particular row, it will close the popup and I expect to see few data from that row appeared on my original page (in the textboxes/labels)
I hope my explanation above clears the air, thank you.
I recently wrote something like that: A database handler as aspx file. But i invoked it by using ajax / jquery.
When my aspx file is done, i write something to the response stream, some code, a json string, what ever.
Example:
$.post("yourdatabasehandler.aspx", { name: "John", lastname: "Smith" }, function(data) {
alert("Response from page: " + data);
});
In that example, name and lastname are values that are posted to your site. You can access them like that:
string name = Request.Params["name"]
// Do your database , validation and whatever logic here
Response.Write("Cool dude");
The above javascript will alert "Cool dude" after your databasehandler is done. Inside your javascript you can react to the response how ever you want - For example reload a page.
Hope that helps? Regards
"1. Due to stateless nature of HTTP, I am unable to process and pass
variable between 2 different windows."
You very wrong with this comment to start with, trying MSDN and ASP.Net "How to pass values between ASP.Net Web pages". Passing between Windows only requires a little bit more thought and possibly a little Javascript to refresh a parent windows or cause a postback on a shild window etc.
If you're using a popup window, you can always use QueryStrings to pass a value going to your popup.
window.open("popup_page.aspx?id=" + id + "&name=" + name)
to access it in popup_page.aspx
string sID = Request.QueryString("id");
string sName = Request.QueryString("name");
Update: if you're using IE the this might help you.
function ShowPopup(strMessage)
{
var returnValue= window.showModalDialog("popup_page.aspx");
}
popup_page.aspx
<asp:Button ID="btnReturnValue" runat="server" Text="Proceed" OnClientClick="window.returnValue='some message';window.close();" />
Note: Please note this only works in IE, so I suggest consider using the followings instead:
jQuery
AjaxControlToolkit ModalPopup
I personally suggest the use of jQuery. :)
Thank's for all answers, comments. rates, and feedback. Just now I found a very helpful link here. Basically the answer is based on that particular code. I just need to alter some part.
<script language="javascript">
function GetRowValue(val)
{
window.opener.document.getElementById("ctl00_ContentPlaceHolder1_TextBox2").value = val;
// make sure you change the TextBoxId as respective to your creation
window.close();
}
</script>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<!-- Reserve the code below, as after you configure data source you -->
<!-- will alter this code drastically therefore-->
<!--you have to make sure to paste this code -->
<!-- again inside this Gridview element once you configure your data source -->
<asp:TemplateField>
<AlternatingItemTemplate>
<asp:Button ID="btnSelect" runat="server" Text="Select" />
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Button ID="btnSelect" runat="server" Text="Select" />
</ItemTemplate>
</asp:TemplateField>
<!-- This part must be reserved -->
</Columns>
Also remember to specify the connection string and the sql command in the datasource.
The rest just follow that tutorial and copy paste the code entirely.

How do you tell if your page is running in an iFrame in .Net? [duplicate]

This question already has answers here:
Detect if a page is within a iframe - serverside
(5 answers)
Closed 9 years ago.
I have an ASPX page that is sometimes loaded into an iFrame in my application and sometimes not. From the code behind how can you detect if your page is being loaded from an iFrame.
I sincerely doubt you could do that from the code behind. You could probably do that in JavaScript, but not from the code behind.
If you really need to know from the code behind, you would perhaps provide a Query String parameter when you load the page in the iframe? Something like...
<iframe src ="page.aspx?iframe=true" width="100%" height="300">
</iframe>
Then in code behind you could do something like...
// sometimes my Asp.NET, Java and PHP get a little confused
// so maybe this might throw some no such index type errors,
// but I think it communicates the idea
string iframe = Request.QueryString["iframe"];
if(!string.IsNullOrEmpty(iframe) && iframe.Equals("true")) {
// this page is in the iframe
}
You can't detect this on server-side, on the client-side this java-script code should work.
<script type="text/javascript">
if (top == self)
alert('Not in an iframe');
else
alert('In an iframe');
</script>
You should use JavaScript to do this.

Categories

Resources